conversion from void*

S

saibot

Hi,

the following snippet of code does not compile with gcc 3.3. Why? It
works when I take "typedef test test_t" instead, but this is not what
I want.
I admit that I do not fully understand this "typedef test test_t[1]"
trick which I copied from the GNU MP library. Could somebody please
explain?

TIA,
Tobias

=====================================================================
typedef struct
{
int x;
} test;

typedef test test_t[1];

int main()
{
void* a;
test_t* b;

b = (test_t*)a; /* this does work */
(*b) = *(test_t*)a; /* this does not work: "incompatible types
in assignment" */

return 0;
}
 
I

infobahn

saibot said:
typedef struct
{
int x;
} test;

typedef test test_t[1];

An object of type test_t is an array of one test (i.e. a struct
with one int, x, in it - we'll use your typedef of test to describe
such a struct).
int main()
{
void* a;
test_t* b;

b is a pointer to an array of 1 test. That is, it could also be
defined as:

test (*)b[1]; /* i.e. a pointer to an array of 1 test */
b = (test_t*)a; /* this does work */

This is equivalent to: b = (test (*)[1])a;

and is fine.
(*b) = *(test_t*)a; /* this does not work: "incompatible types
in assignment" */

*b is an array of one test object. You can't assign to arrays,
because they are not modifiable lvalues.
 
L

Lawrence Kirby

On Fri, 11 Feb 2005 13:19:43 +0000, infobahn wrote:

....
b is a pointer to an array of 1 test. That is, it could also be
defined as:

test (*)b[1]; /* i.e. a pointer to an array of 1 test */

Or rather

test (*b)[1]; /* i.e. a pointer to an array of 1 test */

Lawrence
 
I

infobahn

Lawrence said:
On Fri, 11 Feb 2005 13:19:43 +0000, infobahn wrote:

...
b is a pointer to an array of 1 test. That is, it could also be
defined as:

test (*)b[1]; /* i.e. a pointer to an array of 1 test */

Or rather

test (*b)[1]; /* i.e. a pointer to an array of 1 test */

Oops. My apologies. I didn't have my thinking head on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top