unsigned char** argv

A

ais523

I've been wondering about the compatibility of pointers to signed,
unsigned and plain char, and it's not clear to me how interchangeable
they are. Is the following portable?

int main(int argc, unsigned char** argv)

I suspect this would work on most implementations, but I'm not sure how
portable it is. According to a C89 draft, 'A pointer to void shall have
the same representation and alignment requirements as a pointer to a
character type. Other pointer types need not have the same
representation or alignment requirements.'; does this mean that void*
must have the same representation as all three pointer-to-character
types, or at least one of them, or plain char* specifically? This
section might imply that the representation of unsigned char* is the
same as that of char* (as they're both equal to void*), but I'm having
problems working it out. I can't even figure out whether int (*)(int,
char**) (the type of main normally) is compatible with int (*)(int,
unsigned char**) (the declared type here), and whether that would make
the declaration valid. I understand that the need to actually do this
would be limited in practice, but I'm interested in the theory.
 
P

pete

ais523 said:
According to a C89 draft, 'A pointer to void shall have
the same representation and alignment requirements as a pointer to a
character type. Other pointer types need not have the same
representation or alignment requirements.'; does this mean that void*
must have the same representation as all three pointer-to-character
types

Yes.
 
M

Micah Cowan

ais523 said:
I've been wondering about the compatibility of pointers to signed,
unsigned and plain char, and it's not clear to me how interchangeable
they are. Is the following portable?

int main(int argc, unsigned char** argv)

No. In particular, note that the above is /not/ a pointer to any type
of char, but is a pointer to a /pointer/ to a character type.
I suspect this would work on most implementations, but I'm not sure how
portable it is. According to a C89 draft, 'A pointer to void shall have
the same representation and alignment requirements as a pointer to a
character type. Other pointer types need not have the same
representation or alignment requirements.'; does this mean that void*
must have the same representation as all three pointer-to-character
types, or at least one of them, or plain char* specifically?

All of them. But this does not imply "compatibility", which is
different (otherwise, you could assign char* to unsigned char* without
a cast).

Note, too, that even though pointers-to-void and
pointers-to-character-type have the same representation, it still took
an explicit exception in 6.5.2.2#6 to allow one to be passed as the
other, in variadic function arguments.

However, same representation and alignment requirements implies that
you can always covnert one to the other without problems; and you
could even memcpy() one over the other, and it would work as expected
(not entirely sure, actually: but I can't find anything at the moment
to contradict this).
This section might imply that the representation of unsigned char* is the
same as that of char* (as they're both equal to void*), but I'm having
problems working it out. I can't even figure out whether int (*)(int,
char**) (the type of main normally) is compatible with int (*)(int,
unsigned char**) (the declared type here), and whether that would make
the declaration valid.

Not compatible; so the declaration is not valid.

Moreover, int(*)(char *) is not even compatible with int(*)(void
*). Nor, as has already been stated, is char* even compatible with
void*: it's just that they share the same representation, and in
addition, special exceptions have been made for implicit conversion
between them.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top