unsigned char** argv

Discussion in 'C Programming' started by ais523, Mar 24, 2006.

  1. ais523

    ais523 Guest

    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.
    ais523, Mar 24, 2006
    #1
    1. Advertising

  2. ais523

    pete Guest

    ais523 wrote:

    > 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.

    --
    pete
    pete, Mar 24, 2006
    #2
    1. Advertising

  3. ais523

    Micah Cowan Guest

    "ais523" <> writes:

    > 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.
    Micah Cowan, Mar 24, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,541
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    5,905
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,605
    Old Wolf
    Jan 20, 2004
  4. jab3

    char **argv & char *argv[]

    jab3, Dec 4, 2004, in forum: C Programming
    Replies:
    5
    Views:
    642
    Chris Torek
    Dec 8, 2004
  5. oogie
    Replies:
    9
    Views:
    635
    Default User
    Nov 18, 2007
Loading...

Share This Page