C problems...again

Programming, for all ages and all languages.
Post Reply
Curufir

C problems...again

Post by Curufir »

It's structures and extern this time.

I declare and initialise a global array of my structures in one file, then I want to get at that array in another file.

First file (The initialising part):

Code: Select all

...

typedef struct {
   int Obj_ID;
   int Obj_Status;
} Obj;

Obj Obj_Array[] = {{5,1}};

...
Second file (The wanting to use part):

Code: Select all

typedef struct {
   int Obj_ID;
   int Obj_Status;
} Obj;

extern Obj *Obj_Array;

...

In a function

fprintf(stderr, "%d", Obj_Array[0].Obj_ID);
Boom, segmentation fault. If I try printing out the actual address of Obj_Array (Which I'm hoping is &Obj_Array or I need to start over learning about pointers :)) then Obj_Array in both files is pointing at the same address. There's nothing wrong with the array AFAIK, I can manipulate it fine in the file that contains the declaration, the problems start when I try to reach the data in the second file.

Any chance one of the C gurus could tell me what I'm doing wrong? It's probably something silly.
beyondsociety

Re:C problems...again

Post by beyondsociety »

Suggestion: You should put your struct in a <include.h> file, then add the include in your main program. The reason Im saying this is because you dont need to setup your struct in both files.

Try this:

Code: Select all

// Test.h
#ifndef INCLUDE_TEST_H
#define INCLUDE_TEST_H

typedef struct
{
   int Obj_ID, Obj_Status;
} Obj;

#endif

Code: Select all

// Test.c
#include <stdio.h>
#include "test.h"

extern Obj Obj_Array[] = {5, 1};
int main(void)
{

   fprintf(stderr, "%d\n", Obj_Array[0].Obj_ID);
}
xfyhm

Re:C problems...again

Post by xfyhm »

static rings a bell ?
Schol-R-LEA

Re:C problems...again

Post by Schol-R-LEA »

beyondsociety wrote: Suggestion: You should put your struct in a <include.h> file, then add the include in your main program. The reason Im saying this is because you dont need to setup your struct in both files.
While this is a good suggestion from a general design perspective, it doesn't solve the problem; the effect of the inclusion is exactly the same as the original text.
Try this:

Code: Select all

// Test.h
#ifndef INCLUDE_TEST_H
#define INCLUDE_TEST_H

typedef struct
{
???int Obj_ID, Obj_Status;
} Obj;

#endif

Code: Select all

// Test.c
#include <stdio.h>
#include "test.h"

extern Obj Obj_Array[] = {5, 1};
int main(void)
{

???fprintf(stderr, "%d\n", Obj_Array[0].Obj_ID);
}
I'm not certain, but I doubt that using the initialization syntax is going to work with an [tt]extern[/tt] variable. Also, if the variable is one which is going to be used in other places, it may be better to put the extern declaration in the header file for the purposes of sharing it. Again, this won't solve the problem, but it may simplify some of the code.

As for The Poet's suggestion, it makes no sense to me. The only function we see in the example given is main(), and using a static there is pretty pointless. In any case, the whole point of a static (at least in C) is that it isn't shared; it remains in context between calls, but it is local to the function it is declared in.
sdjbg

Re:C problems...again

Post by sdjbg »

mea culpa: http://phoenix.liunet.edu/~mdevi/c/StaticGlobal.htm

typedef struct {
int Obj_ID;
int Obj_Status;
} Obj;

extern Obj Obj_Array[];

...

In a function

fprintf(stderr, "%d", Obj_Array[0].Obj_ID);
ark

Re:C problems...again

Post by ark »

If you execute the following code:

Code: Select all

fprintf(stderr, "%p", Obj_Array);
with the extern Obj* syntax, then you'll see that the value of the Obj_Array pointer is 5. If you use the extern Obj Obj_Array[] syntax (which you should, because that's how you declared it), the value printed out is the address of the first element in Obj_Array.

That means when you extern Obj_Array as a pointer, you're saying that the memory at the same place as Obj_Array is a pointer, whose value is 5.
Post Reply