0

I've been quite aways away from C and as I am diving back into it I have found myself hitting a roadblock. I have the following structure:

typedef struct{
      char id;
      struct S *children[SIZE];
}S;

In my code I initially declare an array of structs...

 struct S arr[SIZE];

But when I get to this point of trying to allocate my first child for my first member of arr...

 arr[0].children[0] = (S*)malloc(sizeof(S));

I get this warning: warning: incompatible implicit declaration of built-in function ‘malloc’ warning: assignment from incompatible pointer type [enabled by default]

On top of this I'm getting an error that doesn't sound very logical to me. I have the following function:

int foo(S *children[SIZE]);

but when I call this line....

foo(arr[0].children);

I get this note: note: expected ‘struct S **’ but argument is of type ‘struct S **’ which to me just sounds silly, it is expecting the argument it is getting and is upset about it.

Any help in explaining what I should be doing to properly allocate this memory and achieve the same idea would be very much appreciated.

War Gravy
  • 1,543
  • 3
  • 20
  • 32
  • You don't have an array of structs inside your struct. You have an array of pointers. That makes a big difference. – juanchopanza Sep 12 '14 at 06:46
  • 1
    Before you define the type-alias `S`, where do you declare the structure `S`? I'm actually surprised you don't get compiler *errors* as inside the structure there is no structure named `S`. – Some programmer dude Sep 12 '14 at 06:48

2 Answers2

4

There is no struct S, only S which is a typedef of anonymous structure.

Define struct S too:

typedef struct S {
  char id;
  struct S *children[SIZE];
}S;

Or:

typedef struct S S;
struct S {
  char id;
  S *children[SIZE];
};

And do avoid casting return of malloc in C:

arr[0].children[0] = malloc(sizeof(S));
Community
  • 1
  • 1
user694733
  • 15,208
  • 2
  • 42
  • 68
  • So when I go with the second option, it is now telling me it is expecting '{' before ';' and that there is an expected specifier-qualifier-list before '*' token – War Gravy Sep 12 '14 at 06:58
  • On struct definition? Strange. I am not getting compiler warning. Try putting, `struct` back to declaration of children. Cyclical type definitions are a little troublesome sometimes. – user694733 Sep 12 '14 at 07:02
  • Okay I found out why it was giving me that weird one, it was my bad when I was trying something else. But with that second option it not tells me error: ‘S’ redeclared as different kind of symbol – War Gravy Sep 12 '14 at 07:08
  • 2
    @WarGravy you have some other code which also defines `S` then. – M.M Sep 12 '14 at 07:10
  • Yeah so in the compiler, it tells me where my reference is, and it is saying that Struct S{...}; was defined on the line: typedef struct S S; – War Gravy Sep 12 '14 at 07:17
  • The good thing is my previous errors are gone now and it is just this last duplicate reference thing. – War Gravy Sep 12 '14 at 07:18
  • Ah so I was able to get the first one to work. Thank you everyone! – War Gravy Sep 12 '14 at 07:21
  • I do remember one embedded compiler (but not what it was) that deviated from C standard: It didn't use separated namespaces for structures. It meant that `struct S` and `typedef ... S` would collide. I had use different names for those like `typedef struct SS TS`. – user694733 Sep 12 '14 at 08:18
1

For your first problem, you need to do:

#include <stdlib.h>

at the top of your program, in order to call malloc successfully.

The second problem (as also pointed out by others) is that struct S in your class definition refers to a different struct than S. In C, struct tags are in a different "namespace" than type names.

M.M
  • 138,810
  • 21
  • 208
  • 365