Link to home
Start Free TrialLog in
Avatar of zizi21
zizi21

asked on

Dereferencing the pointer to incomplete types

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);

Avatar of josgood
josgood
Flag of United States of America image

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.
Avatar of zizi21
zizi21

ASKER

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......
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);
}
Avatar of zizi21

ASKER

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!
Avatar of zizi21

ASKER

for the search function...typo...

search(ptr head, char *data){

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

strcpy(new1->data,data);
Avatar of Infinity08
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.
>> 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.
ASKER CERTIFIED SOLUTION
Avatar of josgood
josgood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of zizi21

ASKER

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...
Avatar of zizi21

ASKER

just saw this...let me try..thanks
>> 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 ?
>> i test the code and run it..

Which code ?
Avatar of zizi21

ASKER

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....



>> 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 ?
Avatar of zizi21

ASKER

gcc ansi wall pedantic...
>>>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!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zizi21

ASKER

it is working fine now:-)

why does the gcc option has to do with declaration...
>> 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.
Were you able to get it to work with the function calls, and the different source files, as you originally wanted to do ?
Avatar of zizi21

ASKER

i am trying now...
Keep me updated of your progress ... and of your questions/problems if you have any ... :)
Avatar of zizi21

ASKER

thank you.
Avatar of zizi21

ASKER

i just able to do in few files now and it is working. thank you
Great :)