sizeof

C

cs

#include <stdio.h>
#include <stdlib.h>

typedef struct{
unsigned a;
unsigned *b;
unsigned k[50];
}tp;

tp e, *ee;
is it true *e.b==*(e.b)?
is it safe to write sizeof e, sizeof e.a, sizeof e.b, sizeof *e.b,
sizeof e.k in C code?

and if sizeof(unsigned)==4 and sizeof(unsigned*)==8 then

sizeof e.a=4
sizeof e.b=8
sizeof *e.b=4
sizeof e.k=50*4

is it true ?

in the case ee=&e;
is it true *ee->b==*(ee->b)?
is it safe to write sizeof ee->a, sizeof ee->b, sizeof *ee->b,
sizeof ee->k in C code?

sizeof ee->a=4
sizeof ee->b=8
sizeof *ee->b=4
sizeof ee->k=50*4
Thanks
 
E

Emmanuel Delahaye

cs wrote on 17/07/05 :
#include <stdio.h>
#include <stdlib.h>

typedef struct{
unsigned a;
unsigned *b;
unsigned k[50];
}tp;

tp e, *ee;
is it true *e.b==*(e.b)?

Dunno. Without further information, the value of the e.b pointer is
undefined, hence *e.b invokes an undefined behaviour.
is it safe to write sizeof e, sizeof e.a, sizeof e.b, sizeof *e.b,
sizeof e.k in C code?
Yes.

and if sizeof(unsigned)==4 and sizeof(unsigned*)==8 then

sizeof e.a=4
sizeof e.b=8
sizeof *e.b=4
sizeof e.k=50*4

is it true ?
Yes.

in the case ee=&e;
is it true *ee->b==*(ee->b)?

b being still undefined, same answer than above.
is it safe to write sizeof ee->a, sizeof ee->b, sizeof *ee->b,
sizeof ee->k in C code?

sizeof ee->a=4
sizeof ee->b=8
sizeof *ee->b=4
sizeof ee->k=50*4

Yes.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
L

Lawrence Kirby

cs wrote on 17/07/05 :
#include <stdio.h>
#include <stdlib.h>

typedef struct{
unsigned a;
unsigned *b;
unsigned k[50];
}tp;

tp e, *ee;
is it true *e.b==*(e.b)?

Dunno. Without further information, the value of the e.b pointer is
undefined, hence *e.b invokes an undefined behaviour.

True, however *e.b and *(e.b) are equivalent expressions. Since . has
higher precedence than * the e operand is bound to the . operator.

Lawrence
 
C

cs

cs said:
#include <stdio.h>
#include <stdlib.h>

typedef struct{
unsigned a;
unsigned *b;
unsigned k[50];
}tp;

tp e, *ee;
is it true *e.b==*(e.b)?

Dunno. Without further information, the value of the e.b pointer is
undefined, hence *e.b invokes an undefined behaviour.

it seems i understand
tp e;
tp *e=&e;
size_t s0, s1;
s0=sizeof *e.b; s1=sizeof *ee->b;
is ok and safe. Is it right?
Thank you very much.
 
B

Barry Schwarz

cs said:
#include <stdio.h>
#include <stdlib.h>

typedef struct{
unsigned a;
unsigned *b;
unsigned k[50];
}tp;

tp e, *ee;
is it true *e.b==*(e.b)?

Dunno. Without further information, the value of the e.b pointer is
undefined, hence *e.b invokes an undefined behaviour.

it seems i understand
tp e;
tp *e=&e;

You must mean *ee here.
size_t s0, s1;
s0=sizeof *e.b; s1=sizeof *ee->b;
is ok and safe. Is it right?

e.b and ee->b refer to the same variable. Consequently, the two
expressions are equivalent. Furthermore, since sizeof does not
evaluate its operand, it is safe to use either expression as the
operand. This would even be true if ee were not initialized.

The point ED was making was that since you did not initialize e.b, any
attempt to dereference it, as with *e.b, invokes undefined behavior.
This is true with or without the parenthesis.



<<Remove the del for email>>
 

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

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,192
Latest member
KalaReid2

Latest Threads

Top