what are *tt different from **tt?

This is a discussion on what are *tt different from **tt? within the C Programming forums, part of the General Programming Boards category; Code: char *tt = "A"; printf("%d\n",*(unsigned char *) tt); printf("%d\n",*tt); result: 65 65 why are they same ? what are ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    what are *tt different from **tt?

    Code:
     
        char *tt = "A";
        printf("%d\n",*(unsigned char *) tt);
        printf("%d\n",*tt);
    result: 65
    65

    why are they same ?
    what are *tt different from **tt?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by zcrself
    why are they same ?
    Why would they not be the same?

    Quote Originally Posted by zcrself
    what are *tt different from **tt?
    In this case **tt does not make sense, but if tt is a pointer to a pointer, then *tt is a pointer and **tt is what that pointer points to.

    By the way, since "A" is a string literal and string literals must not be modified, you should have written:
    Code:
    const char *tt = "A";
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't see **tt in the code you posted.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by laserlight View Post
    Why would they not be the same?


    In this case **tt does not make sense, but if tt is a pointer to a pointer, then *tt is a pointer and **tt is what that pointer points to.

    By the way, since "A" is a string literal and string literals must not be modified, you should have written:
    Code:
    const char *tt = "A";
    Code:
        printf("%d\n",*(unsigned char *) tt);
        printf("%d\n",*tt);
    Their result is same ? why ?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Casting the pointer doesn't change where it points. You've got *tt in the first one, and *tt in the second one, and surprisingly enough those are the same thing.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by tabstop View Post
    Casting the pointer doesn't change where it points. You've got *tt in the first one, and *tt in the second one, and surprisingly enough those are the same thing.
    I don't understand !
    Can you speak detailly?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by zcrself View Post
    I don't understand !
    Can you speak detailly?
    The bit in parentheses is a cast, and tells the compiler to treat the object as a different type. So you can cast 5 to a float like (float)5, and cast 'a' to a char by (char)'a' and so on. It doesn't actually do anything.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    Quote Originally Posted by zcrself View Post
    I don't understand !
    Can you speak detailly?
    Well, tt is a char*, so *tt is a char (depending on the platform that could be a signed or unsigned value). Then *( unsigned char* )tt is (possibly) just casting a char to an unsigned char. Since they're the same size, there's no chance of the value getting truncated or extended in the process (in contrast to a char/size_t conversion, for instance). In other words, assuming an 8-bit signed char containing the value -1, the bit pattern would be:

    11111111

    Casting it to an unsigned char reinterprets it as 255, which has a bit pattern of:

    11111111

    Same data, different interpretation.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Sebastiani View Post
    Well, tt is a char*, so *tt is a char (depending on the platform that could be a signed or unsigned value). Then *( unsigned char* )tt is (possibly) just casting a char to an unsigned char. Since they're the same size, there's no chance of the value getting truncated or extended in the process (in contrast to a char/size_t conversion, for instance). In other words, assuming an 8-bit signed char containing the value -1, the bit pattern would be:

    11111111

    Casting it to an unsigned char reinterprets it as 255, which has a bit pattern of:

    11111111

    Same data, different interpretation.
    I see
    thanks your answer

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by tabstop View Post
    The bit in parentheses is a cast, and tells the compiler to treat the object as a different type. So you can cast 5 to a float like (float)5, and cast 'a' to a char by (char)'a' and so on. It doesn't actually do anything.
    thanks!
    you do say clearly

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Quote Originally Posted by Sebastiani View Post
    ...Since they're the same size, there's no chance of the value getting truncated or extended in the process (in contrast to a char/size_t conversion, for instance)...
    Ah, but remember that we are talking about pointers here.
    Presumably, all pointers have the same size (I think we can pretty much rely on that), so it doesn't do anything bad changing the type of the pointer.

    We must remember that in memory, all data are just bits: there is no "type". Type is something the compiler has invented to make our lives easier. Different types represent how the compiler stores and reads that raw information and presents it to us.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > Presumably, all pointers have the same size (I think we can pretty much rely on that)
    nope, can't rely on that.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Oh? Can you point out where we cannot rely on this?
    But rely, I mean that we can probably be sure it works in most cases*.

    *) Meaning that it may not be written in the standard, but 99% of the platforms have the same size on all pointer types (except perhaps void*).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21