?
Solved

Dereferencing the pointer to incomplete types

Posted on 2007-07-20
26
Medium Priority
?
498 Views
Last Modified: 2010-04-15
Hi,
I have a struct like this:

typedef struct NODE *ptr;

typedef struct node{

char *data;
ptr next;
}NODE;

.....
ptr new=malloc(sizeof(ptr));

when i tried the strcpy, i got the error dereferencing the pointer to incomplete types...pls advice..thanks

strcpy(new->data,data);

0
Comment
Question by:zizi21
  • 11
  • 10
  • 4
  • +1
26 Comments
 
LVL 13

Expert Comment

by:josgood
ID: 19537403
When I tried this example in VS2005, I encountered several problems.  I came up with

#include <stdlib.h>
#include <string.h>

typedef struct node *ptr;

typedef struct node{
   char *data;
   ptr next;
} NODE;


void main() {
   ptr aNode = (ptr)malloc(sizeof(ptr));
   char* data = "My data string";
   strcpy(aNode->data,data);
}

"new" is a keyword for my compiler, hence the renaming to "aNode".  The main fix I did was to the typedef  struct node *ptr; which I think you meant to use inside the typedef struct node{.
I changed
    link next;
to
   ptr next;
since it looked as though you were trying to build a singly-linked list.
0
 

Author Comment

by:zizi21
ID: 19537432
Hi ,

yup..i am trying to build a singly linked list...
actually, the link next was a typo..so, i edited the previous version and reposted the question..apologies..

my problem is that i cant access any data declared in the struct..the error that i get is dereference......
0
 
LVL 13

Expert Comment

by:josgood
ID: 19537450
The following code works for me.  The key change is
   aNode->data = (char *)malloc(sizeof(data)+1)
which initializes the pointer.  That's not the problem you're having.

Could you modify it to demonstrate the problem you're encountering?

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

typedef struct node *ptr;

typedef struct node{
   char *data;
   ptr next;
} NODE;


void main() {
   ptr aNode = (ptr)malloc(sizeof(ptr));
   char* data = "My data string";
   aNode->data = (char *)malloc(sizeof(data)+1);
   strcpy(aNode->data,data);
   printf("%s",aNode->data);
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:zizi21
ID: 19537473
sure...thanks for looking into this...

i have a main file...

void main(){

ptr aNode;
char data[40]="Testing";

initFunction(aNode); this just init it to NULL aNode=NULL;

search(aNode,data);

}

file1.c file

typedef struct node *ptr;

typedef struct node{
char *data;
ptr next;
}NODE;

i also have function's prototypes used in the main function....

another file, called file2.c
#include "file1.c"

initFunction....

search(ptr head,char *data){

strcpy(head->data,data);

}

thanks!
0
 

Author Comment

by:zizi21
ID: 19537475
for the search function...typo...

search(ptr head, char *data){

ptr new1=(ptr)malloc(sizeof(ptr));
.....check whether not null...

strcpy(new1->data,data);
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19537485
As josgood already said : do not use new as a variable name. If you make that modification, it should already compile a lot better.

But there are a few problems with the code he posted - I fixed them here with comments added :


#include <stdlib.h>
#include <string.h>

typedef struct node *ptr;

typedef struct node {
  char *data;
  ptr next;
} NODE;


int main() {   /* <--- main always has to return int !! */
  ptr aNode = (ptr) calloc(1, sizeof(NODE));   /* <--- use the size of the struct, not the pointer !! */
  char* data = "My data string";
  aNode->data = (char*) calloc(strlen(data) + 1, sizeof(char));  /* <--- allocate memory for the data field before using strcpy on it !! */
  strcpy(aNode->data, data);
  return 0;   /* <--- main always has to return int !! */
}


Note that using this typedef :

    typedef struct node *ptr;

confuses things a LOT. It's better to use a better name for it :

    typedef struct node *NodePtr;

    typedef struct node {
      char *data;
      NodePtr next;
    } Node;

You don't even need the typedef, as this is equally clear :

    typedef struct node {
      char *data;
      struct node *next;
    } Node, *NodePtr;

which I prefer.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19537491
>> initFunction(aNode); this just init it to NULL aNode=NULL;

Why not simply do :

    ptr aNode = NULL;

???


>> file1.c file
>> 
>> typedef struct node *ptr;
>> 
>> typedef struct node{
>> char *data;
>> ptr next;
>> }NODE;

How can your main file "see" these typedefs ?


>> another file, called file2.c
>> #include "file1.c"

You shouldn't include .c files. Instead, place the API in a .h file, and include that. With your setup, that would be something like :

---- file1.h ----

typedef struct node *ptr;

typedef struct node{
char *data;
ptr next;
}NODE;

/* prototypes : */

void initFunction(ptr head);

search(ptr head,char *data);


---- ----

---- file1.c ----

#include "file1.h"

void initFunction(ptr head) {
    head = NULL;
}

search(ptr head,char *data){
    ptr new1 = (ptr) malloc(sizeof(ptr));
    strcpy(head->data, data);
}

---- ----

---- main.c ----

#include "file1.h"

void main(){

ptr aNode;
char data[40]="Testing";

initFunction(aNode); this just init it to NULL aNode=NULL;

search(aNode,data);

}

---- ----


Note that I did NOT fix any of the mistakes that I already reported in my previous post - I leave that up to you.

There's also one other BIG mistake in your code : the functions work with a local copy of the parameters, so any modifications you do to them will NOT be visible in main. For example, the call :

        initFunction(aNode);

will NOT set aNode to NULL. It will set the local copy inside the initFunction function to NULL, but that change will NOT be done to aNode.

Try to make the modifications I already suggested in my previous post, and also try to fix the mistakes I just mentioned. Then post your code here, and we'll see if there are still things to fix.
0
 
LVL 13

Accepted Solution

by:
josgood earned 1000 total points
ID: 19537509
Infinity08, your improvements are well noted and I generally agree with them.  ziz21, I commend them to your attention.  

However, I prefer to make minimal changes when working with presented code.  

I put all the code in a single file to make it easier to work with.  The main issues were
1)  aNode needs a memory allocation.  Setting it to zero makes a bad pointer.
2)  initFunction needs a pointer to a ptr, so that it can place an address in ptr.
3)  search needs another memory allocation, so there is a place to put the string.

I did not encounter an error dereferencing the pointer to incomplete types

Well, you have two competing solutions.  Take your pick.

typedef struct node *ptr;

typedef struct node{
   char *data;
   ptr next;
}NODE;

initFunction(ptr* pNode) { *pNode = (ptr)malloc(sizeof(NODE)); }

search(ptr head,char *data) {
   head->data = (char *)malloc(strlen(data)+1);
   strcpy(head->data,data);
}

void main(){
   ptr aNode = 0;
   char data[40]="Testing";
   initFunction(&aNode);
   search(aNode,data);
}
0
 
LVL 3

Expert Comment

by:asood314
ID: 19537510
The error you're getting is occuring because when you say "typedef struct NODE *ptr" the compiler is expecting you to define a new structure.  When you don't, it complains of an incomplete datatype.  Since NODE is a datatype for struct node, you should say "typedef NODE *ptr."  However, this will not fix your program because NODE isn't actually a datatype until the second typedef statement, and if you move the first typedef statement, then ptr isn't a datatype when you use it in your definition of struct node.  The only way to do it is the way josgood suggested.  The only change I would make is to remove the second typedef because it is unnecessary.
0
 

Author Comment

by:zizi21
ID: 19537520
i test the code and run it..
i get the dereferencing error at two places...

first at aNode->data = (char *)calloc....

strcpy(aNode->data,data);

i also try to put in the struct int such as this int num;
and when i try like this..

aNode->num=3;

i get derefe...error again... Not to sure why, it seems like each time i refer to any member of the struct..the error appears...
0
 

Author Comment

by:zizi21
ID: 19537522
just saw this...let me try..thanks
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19537527
>> Well, you have two competing solutions.  Take your pick.

Well, the code in my last post wasn't correct code, as I said in the same post - it only gave an indication of how to place the code in different files. There were still several issues to fix, as I mentioned in that same post, and in the post before that.

So, it's not a competing solution - rather the same solution, but different approaches of presenting it ;)



btw, zizi21, can you tell us if this is homework or not ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19537528
>> i test the code and run it..

Which code ?
0
 

Author Comment

by:zizi21
ID: 19537552
hi Infinity08,

Not homework but we have another homework which requires us to understand the basics of pointers. So, I thought of learning it  by putting them in different files as previously my codes are usually in one file and that was not good programming practice....

I guess, my mistake was that I should try doing in one file rather than few files...I think, i would stop for a while trying on multiple files...i would concentrate on trying on one file......Maybe, I should try on sample tutorials first before trying myself...

I tested the one that you gave earlier...the one where the whole code is in one file....



0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19537559
>> So, I thought of learning it  by putting them in different files as previously my codes are usually in one file and that was not good programming practice....

A very good idea. But then I suggest first getting the code to work in one file - as you're used to. See josgood's last post (combined with all the other hints in this thread) for that.

Once that works, you can split the code up the way I suggested earlier (using header files).


>> I tested the one that you gave earlier...the one where the whole code is in one file....

And that gave you the dereferencing error ? What compiler are you using ?
0
 

Author Comment

by:zizi21
ID: 19537561
gcc ansi wall pedantic...
0
 
LVL 13

Expert Comment

by:josgood
ID: 19537566
>>>that was not good programming practice

Doctrinaire solutions -- "you should never put all code in one file", "you should always use smart pointers", etc., can lead you astray.  

The key is to have a _reason_ (preferably a good one!) for what you.  There is no one answer that is always the correct answer.

My reason for putting everything in one file was that it made the code easier to post.

Your reason for using multiple files -- gain experience with doing so -- is a good reason.  And as Infinity08 says, its generally a very good idea.  With larger programs and more programmers it becomes a necessity.

I'm off to bed.  Good night, gentlemen.  I'm sure you'll bring this to a successful conclusion.

Cheers!
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 1000 total points
ID: 19537583
>> gcc ansi wall pedantic...

That's one of the reasons I suggested to get rid of the first typedef (see my first post), and place everything in the same typedef :

#include <stdlib.h>
#include <string.h>

typedef struct node {
  char *data;
  struct node *next;
} Node, *NodePtr;

int main() {   /* <--- main always has to return int !! */
  NodePtr aNode = (NodePtr) calloc(1, sizeof(Node));   /* <--- use the size of the struct, not the pointer !! */
  char* data = "My data string";
  aNode->data = (char*) calloc(strlen(data) + 1, sizeof(char));  /* <--- allocate memory for the data field before using strcpy on it !! */
  strcpy(aNode->data, data);
  return 0;   /* <--- main always has to return int !! */
}
0
 

Author Comment

by:zizi21
ID: 19537605
it is working fine now:-)

why does the gcc option has to do with declaration...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19537624
>> why does the gcc option has to do with declaration...

I'm not sure what you mean. But compiling with gcc -ansi -Wall -pedantic means that the ISO standard is followed as strictly as possible, and that all warnings are given. This is a very strict way of compiling.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19537631
Were you able to get it to work with the function calls, and the different source files, as you originally wanted to do ?
0
 

Author Comment

by:zizi21
ID: 19537691
i am trying now...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19537698
Keep me updated of your progress ... and of your questions/problems if you have any ... :)
0
 

Author Comment

by:zizi21
ID: 19544203
thank you.
0
 

Author Comment

by:zizi21
ID: 19544206
i just able to do in few files now and it is working. thank you
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19545147
Great :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question