#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0

    How are you able to reverse a string with pointers?


    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
    	char *pointer = "eggs";
    	char *location;
    	int i = strlen(pointer);
    	for(i;i != 0;i--)
    	{
    		*location = pointer + i;
    		*pointer = *location;
    		pointer++;
    	}
    	printf("%s",pointer);
    }
    I'm trying to get the results of a reversed string pointer (not an array, even though an array is a pointer.)

    why isn't my program working? How am I able to fix it?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    So I found out by testing some more that the reason I had one major error is because I found the null character without even knowing it.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
    	char *pointer = "eggs",*p;
    	int i = strlen(pointer);
    	p = i + pointer - 1; //I needed to add the -1
    	printf("%c",*pointer);
    	//*pointer = *p;
    	printf("%c",*pointer);
    	getchar();
    }
    I added the - 1 so the pointer doesn't have the address of the null character. But when I try to assign it (the line with the comment on it, *pointer = *p) that's when it errors. Why? When I print *pointer it prints "e". How come when I try to reassign it it errors? I don't know how I'm able to fix this. Help?
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    36
    When you declare pointer, you point it to a string literal, and you can't change the value of a string literal any more than you're allowed to suddenly decide that 1 should now be 2. When you do *pointer = *p that is what you're trying to do because *pointer is still pointing to that string literal since you never reassign pointer itself before trying to write to the location in memory it points to.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    Ah, I see what you mean. But how am I able to fix this?

    I was thinking of making another string and manipulating the pointer but you see, this is for an encrypting program.

    Code:
    void encrypt(char *message)
    {
         while(*message) //pointer does not equal 0
         {
          *message = *message ^ 30;
             message++;
         }
    }
    This works but I want to edit it so the pointer, message, can be reversered. If I just make a copy and reverse that the *message pointer is still the same.

    So any ideas how to reverse the pointer?
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    36
    If you want to be able to change what a pointer you pass in points to, you need to add a level of indirection by passing in a pointer to a pointer, or a char** pointer

    Though if you use a proper array of characters containing a c string, instead of a string literal, you can do this without changing any pointers at all and it will be much much simpler.

IMN logo majestic logo threadwatch logo seochat tools logo