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.

2
  • You don't have an array of structs inside your struct. You have an array of pointers. That makes a big difference. Commented Sep 12, 2014 at 6: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. Commented Sep 12, 2014 at 6:48

2 Answers 2

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));
Sign up to request clarification or add additional context in comments.

8 Comments

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
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.
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
@WarGravy you have some other code which also defines S then.
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;
|
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.

1 Comment

Thank you this was very helpful. I did not know about the namespace

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.