Gah. (C issues)

Talk about computers, hardware, applications, and consumer electronics.
User avatar
Global Mod
Global Mod
Posts: 1416
Joined: 2008.09.26 (05:35)
NUMA Profile: http://nmaps.net/user/scythe33
MBTI Type: ENTP
Location: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0

Postby scythe » 2010.02.18 (22:33)

Code: Select all

pool->mutex = { { 0, 0, 0, 0, 0, {  0 } } };
The preceding line gives the error: "expected expression before { token".

Now, what I'm guessing is that the compiler version I'm using doesn't like the way this code creates a pointer to an array. It's not my code, though, so how do I change this so it compiles but acts the same as the preceding? I'm thinking something like

Code: Select all

pool->mutex = & ( {0, 0, 0, 0, 0, { 0 } } );
, but I'm not entirely sure that will work.
As soon as we wish to be happier, we are no longer happy.

Plus (Size) Member
Posts: 42
Joined: 2008.09.27 (02:56)

Postby taaveti » 2010.02.19 (03:58)

You can only set a struct that way on initialization, so while

Code: Select all

pthread_mutex_t mymutex = { { 0, 0, 0, 0, 0, {  0 } } };
is valid,

Code: Select all

pthread_mutex_t mymutex;
mymutex = { { 0, 0, 0, 0, 0, {  0 } } };
is not.
A way around this limitation would be to do something like this:

Code: Select all

pthread_mutex_t mymutex;
pthread_mutex_t tmpMutex = { { 0, 0, 0, 0, 0, {  0 } } };
mymutex = tmpMutex;
but if you are, in fact, using a pthread mutex then you really shouldn't get into the habit of assigning the value of one pthread_mutex_t to another (probably won't hurt with the initializer value, but others could lead to some unfortunate and hard-to-debug problems). In that case, it's probably better to use pthread_mutex_init:

Code: Select all

pthread_mutex_t mymutex;
pthread_mutex_init(&mymutex,NULL);

User avatar
Global Mod
Global Mod
Posts: 1416
Joined: 2008.09.26 (05:35)
NUMA Profile: http://nmaps.net/user/scythe33
MBTI Type: ENTP
Location: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0

Postby scythe » 2010.02.19 (10:26)

It works!

ilu
As soon as we wish to be happier, we are no longer happy.


Who is online

Users browsing this forum: No registered users and 7 guests