0

I'm trying to allocate memory for pointer to array of struct, however it give me a weird error. here's the code:

struct command{
    int type;
    char* input;
    char* output;
    struct command *command[2];
}

when I try to allocate memory for the array size 2, I try:

temp->command = (command*)malloc(sizeof(struct command[2]));

However, I got this error:

incompatible types when assigning to type âstruct command *[2]â from type âstruct command *â

any suggestion?

4
  • temp->command[0] = ... and temp->command[1] = ... Commented Jul 2, 2013 at 16:34
  • The command member is not a pointer to array of a structure, it's an array of two pointers to structures. Commented Jul 2, 2013 at 16:35
  • what are you trying to do? create a 2-element array of commands or a 2-element array of pointers to commands? in any event, in order for the malloc to be satisfied with the current structure definition you need to do the cast like this: (command**) Commented Jul 2, 2013 at 16:38
  • @c-is-best This looks clear to me. An array that contains two allocatable structure pointers. Commented Jul 2, 2013 at 16:39

3 Answers 3

3

You've declared command as a 2-element array of pointer to type struct command, so you don't need to allocate memory for the array, just for each array element, like so:

temp->command[i] = malloc( sizeof *temp->command[i] );
Sign up to request clarification or add additional context in comments.

Comments

1

You need to do like this

for(i=0;i<2;i++)
   temp->command[i] = malloc(sizeof(struct command[i]));

3 Comments

so, when accessing the content of command[0], I can do it with command[0][0] and command [0][1] right?
@JessicaMelia - no, thats wrong. You should better read some basic C texts on pointers and structures.
@Chinna - your solution looks wrong. The malloc(sizeof(struct command[i])) is almost surely wrong. Think again ;-) -- Is this really sooo hard? You have to allocate the plain struct, not an array of that.
0

If you want the element to be a pointer to an array of struct then you can declare and allocate like this...

struct command (*command)[2]; /* note the parentheses */

temp->command = malloc(sizeof *temp->command);

... and if you want the element to be an array of pointers then you can do something like this:

struct command *command[2]; /* equivalent to *(command[2]) */

temp->command[0] = malloc(sizeof *temp->command[0]);
temp->command[1] = malloc(sizeof *temp->command[1]);

(Note that a good way to determine "array of pointer" vs. "pointer to array" is to work your way outwards from the variable name, guided by operator precedence and associativity rules. For example if your declaration is "sometype *myvar[2]" you would start with "myvar is an array of ..." rather than "myvar is a pointer to..." because [] has a higher precedence than *.)

The error message was because your cast indicated type->command was a pointer to struct, which was a conflict since you declared it as an array of pointers to struct. However you shouldn't need to cast unless you are using a C++ compiler.

Comments

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.