-2

I'm trying to define a function that will return a pointer to a structure. I think I followed this correctly, (Returning a struct pointer) but my code keeps complaining with this error message when I try to access the pointer's members, "error: dereferencing pointer to incomplete types".

Here's my code

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

struct lnode  
{  
  char* word;  
  int   line;  
  int   count;  
  struct lnode* nn;     /* nn = next node */  
};

struct lnode* newNode (char* word, int line) 
{
  struct lnode* newn = (struct lnode*) malloc(sizeof (struct lnode));
  if (!newn)
    return NULL;
  strcpy (newn->word, word);
  newn->line  = line;
  newn->count = 1;
  newn->nn    = NULL;
  return newn;
}

int main()
{
  char* p = "hello";
  struct lnode* head = newNode (p, 5);
  //the following lines are causing errors
  assert (!strcmp (head->word, p));     
  assert (head->line  == 5);
  assert (head->count == 1);
  assert (!head->nn);
  return 0;
}

Thanks for the help!

Community
  • 1
  • 1
Joshua
  • 5
  • 3
  • 1
    i have a feeling that sth else is wrong... – Hayri Uğur Koltuk Feb 18 '13 at 15:42
  • 4
    The code you show is not the exactly how you have your code. The structure `lnode` is most likely defined in some other TU. – Alok Save Feb 18 '13 at 15:43
  • 3
    You are going to want to allocate memory for `word`. Your `strcpy` is going to bomb as soon as you can run this. And yes, please show the real code. This code appears to be fake. It's always disappointing to encounter fake code. – David Heffernan Feb 18 '13 at 15:44
  • There is no problem in the code as it is here. – Roee Gavirel Feb 18 '13 at 15:44
  • 2
    `#include ` and don't cast return value of `malloc()`. – hmjd Feb 18 '13 at 15:45
  • @DavidHeffernan http://codepad.org/0xuEHDpN it already did :P `Segmentation Fault`. – Hayri Uğur Koltuk Feb 18 '13 at 15:45
  • Which line is the error on? Is it the struct in the struct? – Neil Feb 18 '13 at 15:48
  • 1
    [Please don't case the return value of `malloc()`, in C](http://stackoverflow.com/a/605858/28169). – unwind Feb 18 '13 at 15:51
  • @user2083727: This code cannot possibly generate the error you quoted. Post real code. – AnT Feb 18 '13 at 15:53
  • @unwind why not cast? I know it's not necessary for C, but it wont stop it working. – Neil Feb 18 '13 at 15:54
  • @Neil: There are lots of things in C that "won't stop it working". That still does not mean that overloading the code with unnecessary clutter is a good idea. There are several reasons not to cast the result of `malloc`, as well as no to use type names under `sizeof`. – AnT Feb 18 '13 at 15:57
  • Thanks for the help! I didn't post the real code, because I thought it would be easier to find the bug, if the code was in one file. Nevertheless you've answered my question. – Joshua Feb 19 '13 at 01:49

2 Answers2

2

Apart from the obvious problem, that you missed to include stdlib.h, there is also a problem with how you handle strings.

In C, you (yes you), have to manage all the memory that you use for strings. This includes the memory pointed to by the member word.

You code does the following (after removing some fluff):

struct lnode* newn = malloc(...);
strcpy (newn->word, word);

Here, newn->word is uninitialized, so this will likely crash.

You will need allocate memory to store the string, for example by calling malloc() a second time:

struct lnode* newn = malloc(...);
newn->word = malloc(strlen(word) + 1);
strcpy (newn->word, word);
Lindydancer
  • 24,366
  • 4
  • 47
  • 67
1

The code must have access to the structure of struct lnode.

As Alok Save hinted, you probably have the declaration in another file (a header perhaps) and you forgot to #include it.

Klas Lindbäck
  • 32,669
  • 4
  • 56
  • 80