Thread: Character pointer problem :

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    1

    Question Character pointer problem :

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<conio.h>
    
    void strrevi(char *start,char *end)
    {
         char *temp;
         if((!start || !end))
         return;
         for(;start<=end;start++,end--)
         {
    //       printf("%c %c %c\n",*temp,*start,*end);
    
           temp=*start;
           *start=*end;
           *end=temp;     
      //     printf("%c %c %c\n",*temp,*start,*end);
           }
    }
    
    
    
    int main()
    {
            char string[20]="some text";
            printf("%s\n",string); 
            strrevi(string,string+strlen(string)-1);
            printf("%s",string);
            getch();
            return 0;
    }
    The problem is in the strrevi function:
    Why doesnt the program work properly if i use
    char *temp in place of char temp and replace every instance of temp with *temp?
    The commented lines may be uncommented for analysis purpose

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Making temp a char * doesn't actually give you a place to store the temporary character for your swap. Besides, even if you had it pointing to a valid character where you could store the data for the swap, you would need to dereference it. Here is how it would look if you properly used temp as a char * (note, this is NOT how I would recommend doing it, it's just for demo purposes).
    Code:
    void strrevi(char *start,char *end)
    {
         char c;
         char *temp = &c;
         if((!start || !end))
         return;
         for(;start<=end;start++,end--)
         {
    //       printf("%c %c %c\n",*temp,*start,*end);
    
           *temp=*start;
           *start=*end;
           *end=*temp;     
      //     printf("%c %c %c\n",*temp,*start,*end);
           }
    }
    Stick with "char temp;"

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kulkarniamit View Post
    Why doesnt the program work properly if i use char *temp in place of char temp and replace every instance of temp with *temp?
    It doesn't work because you have no understanding of pointers.

    type var; /* a variable of type that holds a value */
    type *ptr; /* a pointer to type, that holds an address of type variable */

    ptr = &var; /* put the address of var into ptr as its value */
    something = *ptr; /* get the value of what is at the address ptr holds */


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

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    To perform a swap you need some temporary location to store the value you are swapping.
    If you declare temp as a pointer then what's supposed to hold the actual value you are swapping around?

    There's also no point in your ending condition being start<=end because you don't need to swap the middle character with itself. Just use start < end.

    I also recommend you use the "end is one past the last character" approach as used by C++, which makes for more efficient code.

    Edit: Damn 3 replies in as many minutes. At least we're saying pretty much exactly the same thing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. pointer problem or so...
    By TL62 in forum C Programming
    Replies: 19
    Last Post: 01-12-2008, 11:45 PM
  4. pointer problem
    By peterx in forum C Programming
    Replies: 3
    Last Post: 11-11-2005, 02:02 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM