Thread: Swap 2 words by pointers

  1. #1
    Registered User jaxlle's Avatar
    Join Date
    Nov 2006
    Posts
    9

    Unhappy Swap 2 words by pointers

    hii
    i need to swap 2 words by pointers
    example
    Code:
    char *arr[]={"father","mother",NULL};
    i tried to do like this :
    but i know that char variable can't hold a word . what to do in place
    of temp??
    Code:
    char *p1,*p2;
    char temp;
    temp=*arr;
    *p1=*(arr+1);
    *p2=temp;
    Thank you.

  2. #2
    Registered User marrk's Avatar
    Join Date
    Sep 2006
    Posts
    23

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    You only need one pointer to preform a swap of two elements. What you want to do is to assign the value of one of the elements of the array to the temp pointer (which would be a char *temp), next, move the one you haven't refernced yet to the other location, then move temp back to that one.

    IE:
    Code:
    char *a, *b, *temp;
    //some init of a and b
    temp = b;
    b = a;
    a = temp;
    //done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  2. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  3. Need help in debugging this code
    By jaggesh in forum C Programming
    Replies: 4
    Last Post: 02-09-2008, 08:47 AM
  4. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  5. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 11:25 PM