confusing char pointer

This is a discussion on confusing char pointer within the C Programming forums, part of the General Programming Boards category; if we initialise char pointer as char *p="Hello"; compiler will search for 6 bytes and will return address of first ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,046
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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, 04: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

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