Search code examples
carrayssortingpointersbubble-sort

Pass char** as an argument to a function in C


I know there are many topics of this kind but I've read several of them and still can't figure out what am I doing wrong.

I've successfully generated a char** array. My bubble sort function probably works as well. But when I passed the generated array to the function, only 1 row is copied.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>

void sort(char** tab)
{
        char* temp;
        int i, j, size = sizeof(tab)/sizeof(tab[0]);
        printf("%d\n", size);

        for(i = 0; i < size; ++i)
        {
                for(j = i+1; j < size; ++j)
                {
                        if(strcmp(tab[j-1], tab[j]) > 0)
                                strcpy(temp, tab[j-1]),
                                strcpy(tab[j-1], tab[j]),
                                strcpy(tab[j], temp);
                }
        }
        for(i = 0; i < sizeof(tab)/sizeof(tab[0]); ++i)
                puts(tab[i]);
}

int main()
{
        srand(time(NULL));
        int size = rand()%5+5, i, j, s;
        char** tab = (char**)malloc(size * sizeof(char*));

        for(i = 0; i < size; ++i)
        {
                s = rand()%9+1;
                tab[i] = (char*)malloc(s+1);
                for(j = 0; j < s; ++j)
                        tab[i][j] = 'a'+rand()%26;
                tab[i][s] = 0;
        }
        for(i = 0; i < size; ++i)
                puts(tab[i]);
        puts("");
        sort(tab);
        return 0;
}

Here's how the code works.

And when I write size=5 before the loop in the function it returns segmentation fault.

Edit: Same with passing the size of the array as an argument: http://ideone.com/3Wvncq

Final code

I've fixed all the problems and here's the final code. I was misinterpreting segmentation fault as the result of assigning a fixed size instead of not allocating the temp variable. Thank you for all the answers.


Solution

  • Don't calculate size inside function void sort(char** tab) . As in this function it will be calculated as -

    int i, j, size = sizeof(tab)/sizeof(tab[0]);   // equivalent to sizeof(char **)/sizeof(char*) in function giving wrong length as you desire.
    

    It's length in main(size is generated using rand so no need to find it) and then pass it as argument to function sort.

    Declare your function like this -

    void sort(char** tab,size_t size) 
    

    And while calling from main pass length of tab to it -

    sort(tab,size);  // size will be number of elements in tab calculated in main
    

    You get segmentation fault because of this -

        if(strcmp(tab[j-1], tab[j]) > 0)
                     strcpy(temp, tab[j-1]),         
                     strcpy(tab[j-1], tab[j]),       
                     strcpy(tab[j], temp);
    

    temp is uninitialized in sort and still you pass it to strcpy thus undefined behaviour . Initialize temp before passing to strcpy.Allocate memory to temp in function sort.