Thread: confusing char pointer

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    confusing char pointer

    if we initialise char pointer as
    char *p="Hello";

    compiler will search for 6 bytes and will return address of first
    byte to pointer p.Then how we can print value of p as
    printf("%s",p);
    instead of
    printf("%s",*p);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    p is a char*, so the correct format string in printf is %s
    Eg printf( "string is %s\n", p );

    *p is a char, so the correct format string in printf is %c
    Eg printf( "First char of string is %c\n", *p );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    I assume that %s will print a char at each address of 'p', starting from the first, until '\0' is found. That is why you pass the location (p) not the contents (*p)

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Actually, with the code
    Code:
    char *p = "Hello";
    the compiler sets aside some memory for the string "Hello", along with all other string constants used in the program. p is then assigned the address of the first char in the string, that is, 'H'.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    we do not give address of variable while printing
    [code]
    char a[]="Hello";
    char *p="Hello";
    printf("%s",a);
    printf("%s",p);//Why we are passing address of first constant string ie.H instead of *p
    [\code]

  6. #6
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    //Why we are passing address of first constant string ie.H instead of *p
    Because you're printing a string of characters, not a single character. If you give printf the value of the address using *p, then how would you propose getting to the next address?
    Just because I don't care doesn't mean I don't understand.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    More examples
    Code:
    char a[]="Hello";
    char *p="Hello";
    printf("%s",a);
    printf("%s",p);
    printf("%s",&a[0]);
    printf("%s",&p[0]);
    
    printf("%s",a+2);
    printf("%s",p+2);
    printf("%s",&a[2]);
    printf("%s",&p[2]);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    One that always tickles me is this:

    Code:
    char s[] = "hippo", *p = s+2;
    
    printf("%c\n", p[-1]);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM