Swapping strings in an array of strings

This is a discussion on Swapping strings in an array of strings within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> void swap(char* s1, char* s2) { char* temp = s1; s1 = s2; ...

  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,185
    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
    21,450
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:16 AM
  5. declaring array of strings dynamically!!!!
    By Real in forum C Programming
    Replies: 1
    Last Post: 08-29-2003, 06:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21