Silly Pointer Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinothg
    New Member
    • Oct 2006
    • 21

    Silly Pointer Problem

    Hi ,

    I just wanna know why such a wierd output is seen for the following program.I know it is pretty simple but i can't make it.Just a clue could help me to sort out the issue.

    Code:
    #include <iostream.h>
    #include <stdio.h>
    
    void func(char *s)
    {
       printf("%x \n",s);
       s="senthil";
       printf("%s %x\n",s,s);
    }
    
    int main()
    {
       char* s ="manikandan";
       printf("%s %x\n",s,s);
       func(s);
       cout<<s;
    }
    zcarh0s5-135> !.
    ./a.out
    manikandan 4eb0
    4eb0 >>>>>>>>>> Same address is passed to func()
    senthil 4ea0 >>>>>Here the address is diff !!!
    manikandan


    Can't we change the value of a string using char pointer.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by vinothg
    Can't we change the value of a string using char pointer.
    No.

    You initialised a pointer to an address in memory
    You printed the contents of that address and the address itself
    In the function
    You print the address again
    You change the value of the pointer passed to the function to a different address in memory
    You printed the contents of the new address and the new address itself

    Note that the function changes nothing in main, the original variable s still points to the original address.

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      you need to use strcpy() or strncpy() so
      Code:
      #include <iostream.h>
      #include <stdio.h>
      
      void func(char *s)
      {
      printf("%x \n",s);
      strcpy(s,"senthil");
      printf("%s %x\n",s,s);
      
      }
      int main()
      {
      char   s[] ="manikandan";
      printf("%s %x\n",s,s);
      func(s);
      cout<<s;
      cin.get();
      }
      note that you have to define an array in the calling program of sufficent size to receive the charcaters

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Originally posted by vinothg
        Can't we change the value of a string using char pointer.
        Yes if you do it this way
        Code:
        void func(char *s)
        {
        	printf("%x \n",s);
        	strcpy(s, "senthil\0");
        	printf("%s %x\n",s,s);
        
        }
        int main()
        {
        	char s[50] ="manikandan\0";
        	printf("%s %x\n",s,s);
        	func(s);
        	cout<<s;
        	getchar();
        	return 0;
        }

        Comment

        • LSB
          New Member
          • Jan 2007
          • 8

          #5
          I would suggest to run your code in debugger: you will see what your pointers are and what data they represent.
          Very, very useful !!!

          Comment

          Working...