HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- struct datatype scope question (dereferencing poin...
Operating System - HP-UX
1830366
Members
2152
Online
110001
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2002 12:53 PM
11-16-2002 12:53 PM
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2002 05:25 PM
11-16-2002 05:25 PM
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2002 06:24 PM
11-16-2002 06:24 PM
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...
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2002 01:23 PM
11-17-2002 01:23 PM
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.
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.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP