Pointers problem

This is a discussion on Pointers problem within the C Programming forums, part of the General Programming Boards category; I have a simple question. Let's say I want to change all the spaces in a given string with a ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    67

    Pointers problem

    I have a simple question. Let's say I want to change all the spaces in a given string with a '+' character. I have to do this with pointers.

    Code:
     void change(char *ps){
             while((*ps++)!='\0'){
                           if(*ps==' '){
                                      *ps='+';  // this seems to be wrong ...But how do I change the 
                                                     //value that the pointer is pointing to??
                            }
                       }
       }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Yes, you did it right. You dereference what it points to and change it. However, you skip the first character checking for a space. Maybe a do-while loop for kicks. Also, just for fun, '\0' is the same as 0, so you don't need to explicitely check for it. Re-arranged, it might look like:

    Code:
    	do {
    		if(*ps == ' ')
    			*ps = '+';
    	} while(*ps++);

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    I still get the segmentation fault

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Aaaaaaaaaa...thanks man! I was using a char *str instead of char str[]....

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    Why can't you use char* str instead of char str[] here? As long as you made sure the string was null terminated, there shouldn't be a problem - should there?

  7. #7
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    If you declare char *str = "foo"; you can't then modify it with str[0]. This is because attempting to modify a string literal yields undefined behaviour. In modern operating systems, a string literal will often be stored in read-only memory, causing a violation when your program attempts to change it. See the FAQ posted above.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Korg
    Why can't you use char* str instead of char str[] here? As long as you made sure the string was null terminated, there shouldn't be a problem - should there?
    Stop trolling. No one can be this stupid. Or are you just lazy? Damnit man, you were already given a FAQ link on it!


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Quote Originally Posted by Korg
    Why can't you use char* str instead of char str[] here? As long as you made sure the string was null terminated, there shouldn't be a problem - should there?
    The char *str is null terminated too, but...see cwr's post above.

    Thanks guys for clearing that to me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 11:03 AM
  2. Problem with pointers
    By kotoko in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 06:17 AM
  3. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 05:14 AM
  4. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 10:06 PM

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