Search code examples
cmultidimensional-arrayreversedeclarationc-strings

How to reverse every string in an array of strings through a function in C?


I have been trying to solve this issue for whole day, and could not do it on my own. Searching the internet didn't help me solve it either

So, this the function prototype:

void invert(char **arr, int n);

First argument is an array of strings, and the second one is number of strings in an array.

This is my code:

#include <stdio.h>
#include <string.h>
void invert(char** arr, int n)
{
    int i, j, len;
        for(j=0;j<n;j++)
        {
            len=strlen(arr[j]);
             for(i=0;i<len/2;i++)
                {
                    char tmp = arr[j][i];             
                    arr[j][i] = arr[j][len - i - 1]; 
                    arr[j][len - i - 1] = tmp; 
                }
        }
}
int main()
{
    int n=3, i;
    char **arr;
    arr[0]="John";
    arr[1]="Doe";
    arr[2]="Programmer";
    invert(arr, n);
    for(i=0;i<3;i++)
    {
        printf("%s ",arr[i]);
    }
}

The code breaks when it reaches the line:

arr[j][i] = arr[j][len - i - 1];

and I can't figure out why. The function receives an array of strings perfectly (tested it with some printf statements for characters of specific strings), and the char tmp succesfully recieves a correct character, but the program crashed when it reaches the line mentioned earlier. Printf statements after that line don't work.

Did I miss anything? Can someone explain what am I doing wrong? Thank you!


Solution

  • For starters this code snippet

    char **arr;
    arr[0]="John";
    arr[1]="Doe";
    arr[2]="Programmer";
    

    invokes undefined behavior because the pointer arr is uninitialized and has an indeterminate value.

    Moreover this approach in any case is wrong because you may not change string literals.

    What you need is to declare a two-dimensional array as for example

    enum { N = 11 };
    
    //...
    
    char arr[3][N] =
    {
        "John", "Doe", "Programmer"
    };
    

    In this case the function declaration will look like

    void invert( char arr[][N], int n );
    

    The enumeration must be declared before the function declaration.

    Instead of the two-dimensional array you could declare an array of pointers like

    char s1[] = "John";
    char s2[] = "Doe";
    char s3[] = "Programmer";
    char * arr[3] = { s1, s2, s3 };
    

    In this case the function declaration may be as shown in your question

    void invert(char** arr, int n)
    

    So what you need to do with minimal changes is to substitute this code snippet

    char **arr;
    arr[0]="John";
    arr[1]="Doe";
    arr[2]="Programmer";
    

    for this code snippet

    char s1[] = "John";
    char s2[] = "Doe";
    char s3[] = "Programmer";
    char * arr[3] = { s1, s2, s3 };