Operating System - HP-UX
1830366 Members
2152 Online
110001 Solutions
New Discussion

struct datatype scope question (dereferencing pointer to incomplete type)

 
Max_4
Frequent Advisor

struct datatype scope question (dereferencing pointer to incomplete type)

Hi all,

I have two files. In file1.c I create a user defined datatype (struct datatype).
In file2.c I call a function that returns the datatype created (defined) in file1.c
But the compiler keeps on saying: dereferencing pointer to incomplete type.

Here the contents of file1.c :

#include
#include
#include


struct nlist {
struct nlist *next;
char *name;
char *defn;
};

char *strdup(char *s)
{
char *p;

p = (char *) malloc(strlen(s)+1);
if (p != NULL)
strcpy(p, s);
return p;
}

struct nlist *install(char *name, char *defn)
{
struct nlist *np;

np = (struct nlist *) malloc(sizeof(*np));
if (np == NULL || (np->name = strdup(name)) == NULL)
return NULL;
np->next = NULL;
if ((np->defn = strdup(defn)) == NULL)
return NULL;

return np;
}


Then the contents of file2.c :

#include
#include
#include

#define MAXWORD 100

int getword(char *, int);
struct nlist *install(char *, char *);

int main()
{
struct nlist *np;
char word[] = "MACADDRESS";
char defw[] = "0x2180";

if ( (np = install(word, defw)) != NULL )
printf("np->name : %s :: np->defw : %s\n",
np->name, np->defw);

return 0;
}



When I compile it I have:

$ gcc -g -Wall -ansi -o prog file2.c file1.c
file2.c: In function `main':
file2.c:19: dereferencing pointer to incomplete type
file2.c:19: dereferencing pointer to incomplete type

It complains when I use np->name and np->defn...
Another story is when in just join the file1.c and file2.c in one big file,
then the compiler does not complain.


How can I keep the compiler from complaining when my source is in two files? How can I make file2.c see the datatype definition (struct nlist) in file1.c?


I'd really appreciate any help....

Thanks a lot to everybody for your help....

Max
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor

Re: struct datatype scope question (dereferencing pointer to incomplete type)

The fundamental problem is that file2.c doesn't have a clue what a struct nlist is.

The solution is to createa header file, e.g. myfile.h

that has the struct n_list declared (you might choose to do a typedef) and also declare your extern function there as well.

Both file1.c and file2.c should then #include "myfile.h" and your problems are over.

If it ain't broke, I can fix that.
Max_4
Frequent Advisor

Re: struct datatype scope question (dereferencing pointer to incomplete type)

Thanks a lot Clay... You've been of great help..
I will implement your suggestion...

But after this I just discovered something interesting... It seems that C
does not care about having user defined datatypes with the same names, but located in diferent files at the time of compiling... It's like these names (datatypes names or identifiers... nlist in this case...) are private to the file being compiled.... If I reused "nlist" in the same file, there would be a redefinition, but not when "nlist" is a datatype name located in another file... Just please let me know if I'm wrong with my findings...??

Thanks a lot again...
A. Clay Stephenson
Acclaimed Contributor

Re: struct datatype scope question (dereferencing pointer to incomplete type)

That is absolutely true, you could define 3 different struct nlists in three C source files and C wouldn't complain. In fact, you could declare functions which used these three different struct nlists and the code would compile and link but would probably not execute as expected. You might notice that you could declare a pointer to your struct without ever defining your struct. Pointers are all the same size so that a reference to the base address of a structr works but the compiler has no way to know what the offsets into the fields of the struct are.

C places a great deal of responsibilty directly on the programmer; it's very powerful but with that power comes a lot of responsibility.

You might consider C++; it requires more rigorous type definitions and keeps you out of trouble better.
If it ain't broke, I can fix that.