-1

Writing code c, compiling gcc. I am using global array in main.c and menu.c, in main not working expected. But in menu.c working well. What is a differences.

The global array TXT_STRINGS defined in combo_strings.c

combo_strings.c

#ifndef COMBO_STRINGS_C_
#define COMBO_STRINGS_C_

//array defined here
extern const unsigned char * TXT_STRINGS[50][3]={

{" KAPI SiFRESiNi    GiRiN»","ENTER THE DOOR    ACCESS PIN»"},
{"AYARLAR SiFRESi   GiRiN» ","ENTER SETTINGS    PASSWORD»"},
....

#endif /* COMBO_STRINGS_C_ */

menu.c

//extern array declare
unsigned char * TXT_STRINGS[50][3];

function_x(){
trace_printf(TXT_STRINGS[1][0]); //printing console "AYARLAR SiFRESi   GiRiN"

}

main.c

//extern array declare
unsigned char * TXT_STRINGS[50][3];

main(){
trace_printf("txt_str=%x",TXT_STRINGS[1][0]); //printing console "txt_str=20001f38"

}
  • 1
    Read this: ["How do I use `extern` to share variables between source files in C?"](https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c). – WhozCraig Jun 26 '15 at 13:58
  • `extern type variable_name = ...` simply doesn't make any sense. – Lundin Jun 26 '15 at 14:13
  • You will get ***multiple definition error*** in the code shown in your post. This is because in one file you use `extern const unsigned char *`, then in another, you are using `unsigned char *`. You left off the `const`. The other problem is that type for string literals is not `unsigned char *`, it is `char *`. Both of these issues will prevent a clean compile. – ryyker Jun 26 '15 at 14:14

3 Answers3

2

Really, the best place to create an extern scoped variable
is in a header file, which by convention provides visibility of that extern variable to multiple other source files.

One more point, in your example you define arrays of string literals, which are of type char *, not unsigned char *, this would result in a signed mismatch error on your assignment statement when you compile. For the example below, I will use char *...

Example below illustrates concept:

EDIT (corrected array index sizes to match example)

in some.h

extern const char * TXT_STRINGS[5][2];//declare extern variable

Then in source ONE of your source files define the extern variable

in some.c

//define extern scoped variable in only 1 .c file
const char * TXT_STRINGS[5][2]={{"assdf","assdf"},
                                {"assdf","assdf"},
                                {"assdf","assdf"},
                                {"assdf","assdf"},
                                {"assdf","assdf"}};

And in each source file that needs visibility to TXT_STRINGS:

some.c
#include "some.h"

another.c
#include "some.h"

yetAnother.c
#include "some.h"

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • I tried it also :). getting multiple definition error. @ryyker – saim gokkoyun Jun 26 '15 at 13:57
  • @saimgokkoyun if you are getting a _multiple definition error_, you have an inconsistency in the way you defined variable in .h ***declaration*** and in .c ***definition***. Make sure the array sizes are all the same, and all the types ( i.e. `extern const char * var[3][4][5];` in header, then `const char * var[3][4][5]={...};` in .c ) Remember, the keyword ***extern*** is used only once per variable, in my example, it is used only in .h. – ryyker Jun 26 '15 at 14:08
  • 1
    @saimgokkoyun Do exactly as shown in the above example. You need not declare nor define the variable in the other c files that should use it, simply including the header where the extern declaration resides is enough. – Lundin Jun 26 '15 at 14:15
  • Sorry, i found another problem in my code. Now solved it Thanks you @ryyker . There is a another function getting less paramaters than expected to changing address of TXT_STRINGS. Interesting problem. – saim gokkoyun Jun 26 '15 at 14:36
0

Use the extern keyword

extern unsigned char * TXT_STRINGS[LNG_ROWS_SIZE][LNG_CNT];

in main.c and menu.c and remove it from the header file, so that it's not redefined.

As it is, it's redefined and initialized with 0's, also the appropriate main() signature in this case is int main(void).

Also, don't put the definition in the header file, because if you include it in to source files, and then link the files together, then multiple definition error will prevent compilation.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
0

In your code

  //array defined here

is wrong concept.

You should be

  • using extern for menu.c and menu.c (TXT_STRINGS should appear ar declaration)
  • need to remove extern from combo_strings.c. (TXT_STRINGS will be defined here)

Remember, extern indicates the declaration, not the definition. Your complier should warn you if you're trying to initialize a variable with extern (as you did in combo_strings.c).

That said, to print TXT_STRINGS[VERSIYON][Dil], you need to use %s format specifier, like

 trace_printf("txt_str=%s",TXT_STRINGS[VERSIYON][Dil]);
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261