Like Tree2Likes
  • 1 Post By nonoob
  • 1 Post By tabstop

pointers and strings

This is a discussion on pointers and strings within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> #define MAX 4 int main(){ char str[2][MAX] ={"mom", "dog"}; char *pch; char *pch1; *pch = ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    8

    pointers and strings

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX 4
    
    int main(){
    
       char str[2][MAX] ={"mom", "dog"};
       char *pch;
       char *pch1;
    
       *pch = str[0][1];
    
       *pch1 = pch+MAX;
    
       printf("%c, %p", pch1, pch1);
    
       return 0;
    
    }

    Hi, I want to be able to compare if the middle letters are the same. This is just a snippet. I wanna see if I can add the MAX (4) including the '/O' character to the pointer to get into the next string.

    You know what I mean?

    :P

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That is correct -- adding the size of the second dimension to a pointer will move you "down" a column in your array.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    I don't understand - the value of the middle letters won't affect where the next string is located. The length of the string will.

    edit: Nevermind, I see what you are asking.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Several issues. You should not assign the letter 'o' (or any value) to what pch points to when that pointer was not initialized. Same goes for pch1.
    I think what you meant was:
    Code:
    pch = &str[0][1];
    pch1 = pch + MAX;
    printf("%c, %p", *pch1, pch1);
    Note carefully the absense of '*' in the pointer assignments, the use of '&', and the inclusion of '*' when you wish to display the character.
    Last edited by nonoob; 06-10-2011 at 09:24 AM.
    tabstop likes this.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Good catch -- you want to assign the value of pch, not the value of *pch.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    I also wanted to add:
    Because your array is 4 wide, yes you can add 4 to get to the same place in the next row. It has nothing to do with terminating '\0' in the strings.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Quote Originally Posted by nonoob View Post
    It has nothing to do with terminating '\0' in the strings.
    I think what he was trying to say was that all the text was 3 chars long, so 3 + the NULL would be 4.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    8
    Ah thank you. I kept ending up getting S

    And I was wondering why I was only adding 4 to the ASCII numbers.

    Thank you again.

    I get confused when to put the *.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by mike65535 View Post
    I think what he was trying to say was that all the text was 3 chars long, so 3 + the NULL would be 4.
    Right, but that's not why it works -- it works because the second dimension of the array is 4, regardless of what's actually in the array itself.
    mike65535 likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Pointers / Strings
    By gawallac in forum C Programming
    Replies: 6
    Last Post: 11-01-2007, 06:51 AM
  2. More on pointers and strings
    By mart_man00 in forum C Programming
    Replies: 4
    Last Post: 02-10-2003, 06:10 PM
  3. Help with strings and pointers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-04-2002, 01:17 PM
  4. need more help with pointers and strings
    By bgbfflochp in forum C++ Programming
    Replies: 11
    Last Post: 03-19-2002, 08:31 AM
  5. help with pointers and strings
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2002, 06:54 AM

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