Thread: swap char pointers

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    50

    swap char pointers

    I have the following code.According to this the values of pointers p[0] and p[1] remains unchanged since the swap is made to local variables in swap function.Now my doubt is how can I swap the pointers p[0] and p[1] inside the function swap??
    Code:
    
    
    
    
    
    • #include<stdio.h>
    • int main()
    • {
    • char*p[2]={"hello","good morning"};
    • swap(p[0],p[1]);
    • printf("%s %s",p[0],p[1]);
    • return0;
    • }
    • void swap(char*a,char*b)
    • {
    • char*t;
    • t=a;
    • a=b;
    • b=t;
    • }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    swap needs to make two char **s.

    As a more general solution, write the function

    Code:
    /* swap two memory buffers */
    void memswap(void *a, void *b, size_t len)
    Then you can swap any two variables, structures or buffers in a single call like swap(&x, &y, sizeof(x));
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    Quote Originally Posted by Malcolm McLean View Post
    swap needs to make two char **s.

    As a more general solution, write the function

    Code:
    /* swap two memory buffers */
    void memswap(void *a, void *b, size_t len)
    Then you can swap any two variables, structures or buffers in a single call like swap(&x, &y, sizeof(x));
    This wont work in case of char pointers since you cannot change contents of read only block...

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The first line of Malcolm's post is what you should be looking at.

    Code:
    int a = 0;
    
    printf("a is %d\n",a);
    
    updateVariable(a);
    
    printf("a is %d\n",a);
    
    // ---------------
    
    void updateVariable(int a)
    {
        a = 4;
    }
    (1) Do you understand why this snippet will print "a is 0" twice (not "a is 4" the second time around)?

    (2) Do you know how to modify "updateVariable()" so that it will correctly update the value of a?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Saurabh Mehta
    This wont work in case of char pointers since you cannot change contents of read only block...
    If the char pointer cannot be changed, then you cannot swap it with another char pointer to begin with, no matter what you do. What you are probably thinking of is swapping the entire string literals, but the title of your thread is "swap char pointers", not "swap string literals".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Your swap function ought to accept two pointers to the objects you intend to swap. The type of p[0] and p[1] is pointer to char. You want to swap two pointers to char rather than two chars, so you clearly have a type mismatch. What you want to pass to swap are two char **s. The expressions p + 0 and p + 1 are what you want.

    edit: And, yeah, you need to change the types used in your swap function or, as Malcolm has suggested, write a generic function for swapping any data types and pass in the size manually. Though I should note that 'mem' is a reserved prefix.
    Last edited by Barney McGrew; 04-23-2013 at 03:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swap function and pointers
    By Jdls210 in forum C Programming
    Replies: 5
    Last Post: 12-10-2011, 09:29 AM
  2. how to swap int or char using marcos
    By nkrao123@gmail. in forum C Programming
    Replies: 14
    Last Post: 10-12-2009, 06:01 AM
  3. Need some help on a char swap
    By cwafavre in forum C Programming
    Replies: 9
    Last Post: 11-05-2007, 05:56 PM
  4. Swap 2 words by pointers
    By jaxlle in forum C Programming
    Replies: 2
    Last Post: 01-15-2007, 02:49 PM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 10:06 PM

Tags for this Thread