0

How I can compare 2d arrays of char in C? I tried this

        Char **arr_1;
        Char **arr_2;

        ...// malloc. Its OK. 

        for (I=0; I<n; I++)
        {
           If (strcmp (arr_1[I],arr_2[I])==0)
                // do smth
        }

But it doesn't work . I'm not good in pointers.

Arrays have some array of words.

Ubuntu gcc

Without strcmp, the program works.

Thanks

Artur Ganiev
  • 31
  • 1
  • 5

2 Answers2

2

You can do it.

char arr_1[10][10];
char arr_2[10][10];

int n;
scanf("%d",&n);

for(int i=0; i<n; i++)
{
    scanf(" %[^\n]",arr_1[i]);
    scanf(" %[^\n]",arr_2[i]);
}

for(int I=0; I<n; I++)
{
    if(strcmp (arr_1[I],arr_2[I])==0)
    {
        // do smth
    }
}
Mr. Perfectionist
  • 2,605
  • 2
  • 24
  • 35
0

you must be allocating 2D dynamic array wrong.
consider this code:

int SIZE=5;

char **arr_1;
arr_1 = malloc(SIZE* sizeof(char *)); //initialising an array of pointers

char **arr_2;
arr_2 = malloc(SIZE* sizeof(char *)); //initialising an array of pointers

for(i=0;i<SIZE;i++)  
{
    printf(" Enter a name\n");  
    arr_1[i]=malloc(100*sizeof(char)); //for each pointer in this array allocate an array of characters
    scanf("%99s",arr_1[i]);
}

for(i=0;i<SIZE;i++)  
{
    printf(" Enter a name\n");  
    arr_2[i]=malloc(100*sizeof(char)); //for each pointer in this array allocate an array of characters
    scanf("%99s",arr_2[i]);
}


for(i=0;i<SIZE;i++)  
{
    if(strcmp(arr_1[i],arr_2[i])==0)
   //do smthing 
}
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225