Thread: String Pointer Tracing

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    9

    String Pointer Tracing

    Hey. I am having trouble tracing this code. The output is

    hursday March 30, 2006
    March 30

    but I cannot see why? From what I see, the str_ptr variable points at the String str, and this should point to the first element (T). Then the printf statement should print the letter h because it is incremented. After that it should print elemtent 8, which is y.

    What am I missing here?

    Thanks in advance
    Code:
    #include<stdio.h>
    int main(void) {
    char str[] = "Thursday March 30, 2006", *str_ptr;
    str_ptr = str;
    printf("%s\n", ++str_ptr);
    str[17] = '\0';
    printf("%s\n", str_ptr+7);
    return(0);
    }

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    str[8] is ' ', not 'y'.

    Remember it starts with str[0].

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    oops...but even so, why does it print the entire string after until the null character rather than the character. Would the pointer not only point to a specifc element?

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    >printf("%s\n", ++str_ptr);
    %c for character
    and something like this *(str_ptr)

  5. #5
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    Code:
    printf("%s\n", ++str_ptr);
    You are making ++str_ptr so it will increment pointer first.
    So its pre-increment,so first str_ptr is incremented then it is passed to printf() thats why ur seeing that output.
    hursday March 30, 2006
    March 30
    Try
    Code:
    printf("%s\n", str_ptr++);
    then U'll see result as
    Thursday March 30, 2006
    March 30

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    So with these lines of code,

    Code:
    str_ptr = str;
    printf("%s\n", ++str_ptr);
    str[17] = '\0';
    printf("%s\n", str_ptr+7);
    am I essentially saying print the string from the place which str_ptr is pointing to until the NULL character is reached? Also why would not a defeference * be needed before the ++str_ptr and str_ptr+7?
    Last edited by bobby19; 04-12-2006 at 08:06 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yes.

    And it's called the nul character (\0)
    This is different from the NULL pointer.
    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
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Exactly. That's how prinf() handles char*, and that's how all standard C handles char*, so you'd be better off doing it their way. Remember, a char* is simply a 32(or 64) bit pointer to somewhere in North Dakota.

    --beaten by salem--


    Note that even if your char[] points to "abcdefghijklmnop \\0\\ qrstuvwxyz", when passed to prinf() only "abcdefghijklmnop" will be printed. printf() makes no guesses regarding the actual size of your array, because there is no way to determine this at runtime.
    Last edited by jafet; 04-12-2006 at 08:15 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    Hi,bobby19

    Also why would not a defeference * be needed before the ++str_ptr and str_ptr+7?
    We don't dereference * b4 the ++str_ptr or str_ptr+7 is because we are just incrementing pointer not value inside it.
    * is value of operator. so its not required to dereference * ++str_ptr.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to String and Pointer to Char
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 03:03 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM