LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Can I predefine constant array in c? (https://www.linuxquestions.org/questions/programming-9/can-i-predefine-constant-array-in-c-823350/)

kalleanka 08-13-2010 08:00 AM

works like a charm.

just some questions so i will understand it.

if i do not use extern the compiler will create two pointers one local in .c and one in .h ? And would get faulty logic? Or is it like a forward declaration and extern is implied?

How come i can not use static in .c and/or .h. I only want one instance?

If I use const will the compiler only allocate memory once even its used in several threads or instances?


And just another question. If I have a const int x how do i cast it to int so i do not get warnings? Is it just (int) x ?

Sergei Steshenko 08-13-2010 08:01 AM

Quote:

Originally Posted by kalleanka (Post 4065106)
This is of course the best way. That i did not think about that. And performance might be a problem but i do not know for now. I have the hole system (100M) running in ram (1000M) and its for a kiosk with intel atom.

Thanks a lot. I really appreciate it.

ps I never took a course in c or c++ but i know java, pascal, lisp. C got more issues(memory, pointer, strings etc) for a programmer and i seem to fall into the trap all the time.

It is not the best way, though close to it.

Free your mind from the necessity to have two files (.h and .c) and come to peace with the idea to have just one file. This is what I have in my project - the file conceptually looks like this (let's call it 'data.c'):


Code:

#define NUMBER_OF_ELEMENTS 3

#ifdef INSTANTIATE_DATA
#define EXTERN
#else
#define EXTERN extern
#endif


// now data comes, which can be either just extern declaration or instance

EXTERN const int some_const_int_array[NUMBER_OF_ELEMENTS]
#ifdef INSTANTIATE_DATA
= {0, 1, 2}
#endif
;

When one needs to compile the file in order to create the data objects, he/she compiles it with

Code:

gcc -Wall -Wextra -DINSTANTIATE_DATA -c data.c
.

When one needs the data to become known in a function file (let's call it 'function.c'), he/she writes the file this way:

Code:

#include <stdio.h>
#include "data.c"
void function()
  {
  printf("some_const_int_array[1]=%d\n", some_const_int_array[1]);
  }

and compiles the function file the regular way:

Code:

gcc -Wall -Wextra -c function.c
.

If it's not clear how it works, ask specific questions.

The conceptual advantage is that 'data.c' is both the prototype and the implementation. In fact, I am now thinking of expanding the approach to function files too.

Please note that even in case of data declaration, i.e. in case of effectively telling

Code:

extern const int some_const_int_array[NUMBER_OF_ELEMENTS];
the compiler does know number of elements in the array which improves error checking - 'gcc' catches a lot of index boundaries violations statically.

A declaration like

Code:

extern const int some_const_int_array[];
is much more error prone.

kalleanka 08-13-2010 08:12 AM

Thats a nice sulotion and i do understand it. And i use NUMBER_OF_ELEMENTS in all my arrays since i will get compile errors if i assign the wrong number of elements and thats easy to do.


All times are GMT -5. The time now is 09:40 PM.