Thread: Swapping strings in an array of strings

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    5

    Swapping strings in an array of strings

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void swap(char* s1, char* s2)
    {
        char* temp = s1;
        s1 = s2;
        s2 = temp;
    }
    
    int main()
    {
        char *str_array[4];
    
        char buffer[128];
    
        int i;
        for (i = 0; i < 4; i++)
        {
            printf("Enter string %d: ", i + 1);
            scanf("%s", buffer);
    
            int length = strlen(buffer); //get the length of the inputed string
            ++length; //add a space for the NULL charactor at the end
    
            //allocate space at i in the str_array for the word
            str_array[i] = malloc(length * sizeof(char));
    
            //copy the word into the array
            strcpy(str_array[i], buffer);
        }
    
        //swap first word and second word
        swap(&str_array[0], &str_array[1]);
    
        //display words
        for (i = 0; i < 4; i++)
            printf("%s\n", str_array[i]);
    
        //free allocated memory
        for (i = 0; i < 4; i++)
            free(str_array[i]);
    
        return 0;
    }
    If I enter: car boat plane train I want to to swap car and boat. So it would then display boat car plane train.

    On the line where I call the swap function i get:
    Code:
    warning: passing arg 1 of `swap' from incompatible pointer type
    What is the correct way to go about swapping strings in an array? Thank you for any help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You want to swap strings. &str_array[0] is not a string. str_array[0] is.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Actually, I think that the problem is that this function has no net effect:
    Code:
    void swap(char* s1, char* s2)
    {
        char* temp = s1;
        s1 = s2;
        s2 = temp;
    }
    What you are doing here is swapping the values of the local pointers s1 and s2, but the variables in the caller remain unchanged. What you should do is to have char** parameters instead:
    Code:
    void swap(char** s1, char** s2)
    {
        char* temp = *s1;
        *s1 = *s2;
        *s2 = temp;
    }
    Once you make this change, your original code would be correct.
    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

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Thank you laser light. That worked great.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 12:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. Passing an Array of Strings from VB to a C DLL
    By mr_nice! in forum Windows Programming
    Replies: 9
    Last Post: 03-08-2005, 06:16 AM
  5. declaring array of strings dynamically!!!!
    By Real in forum C Programming
    Replies: 1
    Last Post: 08-29-2003, 06:59 AM