Thread: Pointer to String and Pointer to Char

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Pointer to String and Pointer to Char

    Are Pointer to String and Pointer to char both synonymous?

    char *s;

    is a pointer to char.

    but since a string is a char array, and array name automatically decays to a pointer in assignment, so i could use the same definition for a string

    Confirm if i m right?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes for your last question.

    Remember that a string has to have a '\0' char to denote the end of the string. If you pass a pointer to a single char to a function expecting a pointer to a string, unless that one char is a '\0', you're possibly playing with undefined behavior.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Alternatively one might think that a pointer to string in C is a pointer to a char array / a pointer to a pointer to a char.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    root koodoo's Avatar
    Join Date
    Oct 2005
    Location
    a small village faraway in the mountains
    Posts
    28
    Quote Originally Posted by anon View Post
    Alternatively one might think that a pointer to string in C is a pointer to a char array / a pointer to a pointer to a char.
    Code:
    char *str = "Hello World!";
    str is a pointer to a string.
    str is a pointer to a char array.
    But str is a pointer to a char (not a pointer to a pointer to a char). str is of type char * .(and here str points to H).

    regards,
    koodoo.
    If this helped you, please take the time to rate the value of this post by clicking here.

    Statutory Warning : Beware of C... it's highly addictive.

    --------------------------------------------------------------
    Registered Linux User #388419
    --------------------------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assign a string to char pointer
    By seaking1 in forum C Programming
    Replies: 8
    Last Post: 12-08-2009, 02:54 PM
  2. A Question About Unit Testing
    By Tonto in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 08:22 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM