tags:

views:

977

answers:

6

Hello,

C99 gcc

I keep getting this error. I have a struct outside main. And inside main I am trying to allocate on the stack using calloc. I can't seem to find out what is wrong.

Thanks for any advice,

error: expected expression before ‘)’ token

/* global */
struct port_data_t                                                                      
{                                                                                       
    size_t task_id;                                                                     
    pthread_t *thread_id;                                                               
    size_t start_port;                                                                  
    size_t number_ports;                                                                
} *port_data;                                                                           


/* main function */
struct port_data_t *port_data = (struct task_data_t*) calloc(4, sizeof(port_data*));
A: 

Perhaps it doesn't know what a task_data_t is? Did you mean port_data_t?

jeffamaphone
+3  A: 

should be sizeof(port_data_t) not sizeof(port_data*). The former is the size of a port_data_t struct. The latter doesn't mean anything.

Crashworks
I prefer using *<allocated ptr>, less breakage if the type changes.
Simon Buchan
Should be sizeof(struct port_data_t), not sizeof(port_data_t) unless I'm badly mistaken.
Chris Lutz
A pluser! Kill the heathen pluser! :)
Simon Buchan
+4  A: 

Should be calloc(4, sizeof(*port_data)): Note * before var name.

Simon Buchan
A: 

As pointed out by Jeffamaphone, task_data_t is out of context. And also pointed out by Crashworks, try sizeof (port_data_t) if you want an array of structures into port_data.

Using sizeof (port_data*) will make it allocate sizeof pointer to pointer of port_data_t which in most cases is only so useful in most of the cases.

Alphaneo
I assume you meant `sizeof(port_data_t*)` at the end there...
Simon Buchan
No, he means sizeof(struct port_data_t). I've checked this with GCC and it doesn't work without the struct keyword in there.
Chris Lutz
Too much C++ :'(
Simon Buchan
+1  A: 

Try changing this:

struct port_data_t *port_data = (struct task_data_t*) calloc(4, sizeof(port_data*));

To this:

port_data = (struct port_data_t*) calloc(4, sizeof(*port_data));

Might work a little better. If you've declaring port_data as a global struct, you don't need to re-declare it as a struct port_data_t. GCC should already know that. Of course, how I would do it is this:

port_data = (struct port_data_t*) calloc(4, sizeof(struct port_data_t));

But I don't like putting variables in sizeof(). I try to stick with putting types in there, just out of habit. Plus, it resolves any ambiguities about how exactly a pointer needs to be dereferenced, which is tripping you up in this case.

Chris Lutz
Of course, using the variable is much safer when you are changing the pointer type...
Simon Buchan
+1  A: 
#include <malloc.h>
#include <pthread.h>

typedef struct port_data_t {
    size_t task_id;
    pthread_t *thread_id;
    size_t start_port;
    size_t number_ports;
} port_data_t;

port_data_t* f() {
    port_data_t* ports = (port_data_t*)calloc(4, sizeof(port_data_t));
    return ports;
}