Home > Back-end >  Strcmp 1-D array vs 2-D array in c
Strcmp 1-D array vs 2-D array in c

Time:07-20

When using strcmp function (in the string.h library) , passing a 2-D character array as a parameter comparison , there is a need to add the ampersand symbol like

&mainlist[i][0]

otherwise like

mainlist[i][0]

there's an error

warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast [-Wint-conversion]

versus when passing 1-D character array as a parameter comparison , its enough

mainlist[i]

without ampersand symbol. How come?

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

#define FOUND 1
#define NOT 0

int main()
{
   char mainlist[5][10] = {"test1", "test2", "test3", "test4", "test5"};
   int flag, i;
   flag = NOT;
   char myname[50];
   printf("Enter ur name.\n");
   scanf("%s", myname);
   for(i = 0; i < 5; i  )
   {
      if(strcmp(mainlist[i], myname)== 0) //vs if(strcmp(&mainlist[i][0]), myname)==0)
      {
         flag = FOUND;
         printf("Your name was found welcome.\n");
         break;
        
      }
   }
   if(flag == NOT)
   {
      printf("Your name was not found .\n");
   }
}

CodePudding user response:

When using strcmp function (in the string.h library) , passing a 2-D character array as a parameter comparison , there is a need to add the ampersand symbol like: &mainlist[i][0]

mainlist[i][0] is not a 2-D array, it is the first character of a string: a char. &mainlist[i][0] takes a pointer to that char, a char *. It is just a complicated way to say mainlist[i].

Every array index does a dereference. mainlist is, effectively, a char **. mainlist[i] is a char * and mainlist[i][j] is a char.


char mainlist[5][10] = {"test1", "test2", "test3", "test4", "test5"};

Looks like this in memory.

          1         2         3         4
01234567890123456789012345678901234567890123456789
test1_____test2_____test3_____test4_____test5_____
^
|
mainlist

Where _ represents a null byte. mainlist points at the memory containing the first character.

When you ask for mainlist[i] that says to add i * 10 to the address of mainlist and dereference it. You can literally write *mainlist i * 10

          mainlist[1]         mainlist[3]
          |                   |
          v                   v
          1         2         3         4
01234567890123456789012345678901234567890123456789
test1_____test2_____test3_____test4_____test5_____
^                   ^                   ^
|                   |                   |
mainlist[0]         mainlist[2]         mainlist[4]

A pointer to the first character of a string is what a string is in C, a char *. Each of these are a string, a 1-D array, suitable for passing to strcmp.

mainlist[i][0] says to take the address of mainlist[i], add 0, and dereference it. The result is a char like t. This doesn't work with strcmp because it takes a pointer. The integer in "warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast [-Wint-conversion]" is the char because characters are just integers and strcmp tried to use that integer as a char pointer.

&mainlist[i][0] says to make a pointer to that char, so we're back to a char * or mainlist[i].

Demonstration.

mainlist[i] is almost always preferable to &mainlist[i][0]. However, it does have its uses. There are times when you want to skip the first few characters of a string, and you can do so without copying nor modifying the string. For example, &mainlist[2][2] is "st3". It's a pointer to mainlist[2] 2.

                      &mainlist[2][2]
                      |
                      v
01234567890123456789012345678901234567890123456789
test1_____test2_____test3_____test4_____test5_____
                    ^
                    |
                    mainlist[2]

C will read from the pointer until the null byte.

  • Related