Conversions between character pointers

S

Spiros Bousbouras

int foo(void) {
char c = 0 , *p = &c ;
unsigned char *up = (unsigned char)p ;
signed char *sp = (signed char)up ;

return p == (char)up && p == (char)sp &&
up == (unsigned char)sp &&
sp == (signed char)p ;
}

Is foo() guaranteed to always return 1 ? Based on 6.2.5 p26 and 6.3.2.3
p7 I would say yes.
 
B

Ben Bacarisse

Spiros Bousbouras said:
int foo(void) {
char c = 0 , *p = &c ;
unsigned char *up = (unsigned char)p ;
signed char *sp = (signed char)up ;

return p == (char)up && p == (char)sp &&
up == (unsigned char)sp &&
sp == (signed char)p ;
}

Is foo() guaranteed to always return 1 ? Based on 6.2.5 p26 and 6.3.2.3
p7 I would say yes.

Not without six *s being added in the right places!
 
S

Spiros Bousbouras

Not without six *s being added in the right places!

Sorry , here's correct code :

int foo(void) {
char c = 0 , *p = &c ;
unsigned char *up = (unsigned char *)p ;
signed char *sp = (signed char *)up ;

return p == (char *)up && p == (char *)sp &&
up == (unsigned char *)sp &&
sp == (signed char *)p ;
}
 

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,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top