Compilation Error: dereferencing pointer to incomplete type


 
Thread Tools Search this Thread
Top Forums Programming Compilation Error: dereferencing pointer to incomplete type
# 1  
Old 10-10-2011
Compilation Error: dereferencing pointer to incomplete type

I am getting a dereferencing pointer to incomplete type error when i compile the following code on lines highlighted in red. Can anyone help me in identifying what is wrong in the code?
Code:
#include<stdio.h>
#include<stdlib.h>

typedef struct{
int info;
struct node* link ;
} node;

void main(){
struct node* head;
head = (struct node *) malloc (sizeof(struct node* ));
head->info = 10;
head->link = NULL; 
}

# 2  
Old 10-11-2011
There are many things wrong with your code...the struct typedef lacks a struct tag...and you are mallocing memory for a pointer to that struct instead of a struct object. Fixing these two things should give you a clean compilation...
# 3  
Old 10-11-2011
can you please explain in simple terms what is wrong.. and how to correct it?
# 4  
Old 10-11-2011
Quote:
Originally Posted by sreeharshasn
I am getting a dereferencing pointer to incomplete type error when i compile the following code on lines highlighted in red. Can anyone help me in identifying what is wrong in the code?
Code:
#include<stdio.h>
#include<stdlib.h>

typedef struct node{
int info;
struct node* link ;
} node_t;

void main(){
node_t *head;
head = (node_t *) malloc (sizeof(*head));
head->info = 10;
head->link = NULL; 
}

See in bold.

First, you forgot the struct name, so after "typedef struct" I added "node". This makes it "struct node". This is where you were having the "undefined type" problems, because "struct" without "node" is just an anonymous typedef'd struct.

The second change, I like to suffix typedef'd types with "_t" so they are known to be types. This doesn't have to be your style, but it helps clarify in the rest of the code that you can use either "struct node" or "node_t" to define "objects" of types "node_t". Essentially, the compiler makes "struct node" and "node_t" synonymous.

Some people may also add something like this to the code:

Code:
typedef node_t *node_ptr;

But, I'm not a fan.

As for the other changes, I like making the sizeof in the malloc line to be the size of the object we're allocating. It is "safer" because if you even change the type, you don't need to go hunt out all the malloc's and make sure you change the sizeofs. That's just my personal opinion, though. You could have easily used "sizeof(node_t)". Where you made your mistake (as pointed out by the above poster) was saying "sizeof(node_t *)" (in essence). Which is the size of a pointer not the size of the thing you are pointing to, which is how much space you need.

Last edited by DreamWarrior; 10-11-2011 at 01:56 PM..
This User Gave Thanks to DreamWarrior For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Warning: pointer type mismatch

Hi all, I'm new programming in C, so I had the next message in my code: Dual.c:88:20: warning: pointer type mismatch in conditional expression : &clientSa.sin6.sin6.sin6_addr, Any help would be great #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include... (1 Reply)
Discussion started by: godna
1 Replies

2. Programming

Dereferencing pointer to a shared memory struct

I have what should be a relatively simple program (fadec.c) that maps a struct from an included header file (fadec.h) to a shared memory region, but I’m struggling accessing members in the struct from the pointer returned by shmat. Ultimately, I want to access members in the shared memory structure... (2 Replies)
Discussion started by: arette
2 Replies

3. Programming

Dereferencing pointer to incomplete type

// Hello all, I am having this error "Dereferencing pointer to incomplete type " on these 2 lines: xpoint = my_point->x; ypoint = my_point->y; I am having no clue y this is happening. Any help would be greately appreciated!!!! #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: mind@work
2 Replies

4. Programming

gcc 4.3.2 accept sys call warrning incompatible pointer type

Hi all, this warning is driving me nuts. I use -pedantic with -Wall and -Werror so this needs to be fixed. BUILD: GNU-Linux-x86 Any ideas? struct sockaddr_in server_addr; int addr_len = sizeof (server_addr); fd = accept(link->socket_fd, (struct sockaddr_in *)... (2 Replies)
Discussion started by: personificator
2 Replies

5. UNIX for Dummies Questions & Answers

Build Error: error: dereferencing pointer to incomplete type

I'm getting the following Error: prepare_pcap.c: In function `prepare_pkts': prepare_pcap.c:127: error: dereferencing pointer to incomplete type prepare_pcap.c:138: error: dereferencing pointer to incomplete type ==================================== This is the part of the relevant... (8 Replies)
Discussion started by: katwala
8 Replies

6. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

7. Programming

error: field `fatx_i' has incomplete type

I'm trying to compile a 2.4.26 kernel but I have to apply two patches to it. The patches are: linux-2.4.26-xbox.patch openMosix-2.4.26-1 This is the reason that it doesn't compile. There is only one error but I'm not familiar with C or C++(Unfortunately only Java and some lower-level... (2 Replies)
Discussion started by: lateralus01
2 Replies

8. Programming

error: field has incomplete type

Hello there, Here is how it goes - I have written a small test driver as an exercise to "Linux Device Drivers" and as a preparation for writing a real, functional driver. For the sake of seeing how far I got it working (I already implemented the open(0, read(), write() and ioctl() calls) I... (4 Replies)
Discussion started by: boyanov
4 Replies

9. Programming

array type has incomplete element type

Dear colleagues, One of my friend have a problem with c code. While compiling a c program it displays a message like "array type has incomplete element type". Any body can provide a solution for it. Jaganadh.G (1 Reply)
Discussion started by: jaganadh
1 Replies

10. Programming

Accesing structure member:Error:dereferencing pointer to incomplete type

$ gcc -Wall -Werror struct.c struct.c: In function `main': struct.c:18: error: dereferencing pointer to incomplete type $ cat struct.c #include <stdio.h> #include <stdlib.h> #include <string.h> /*Declaration of structure*/ struct human { char *first; char gender; int age; } man,... (3 Replies)
Discussion started by: amit4g
3 Replies
Login or Register to Ask a Question