-2

I am trying to copy the value of argv[i] (which is a string) into a char array x[10] by using strcpy(). Code:

char x[10];

int main(int argc, char *argv[])
{
   for (int i = 1; i < argc; i++)
    {
        strcpy(x[i],argv[i]);
    }
.....
}

output

expected 'char * restrict' but argument is of type 'char' 
   61 |   char * __cdecl strcpy(char * __restrict__ _Dest,const char * __restrict__ _Source);
heyharshal
  • 13
  • 3

1 Answers1

0

In the above when you type use the strcpy function you pass as the first argument:

x[i]

which evaluates to the following:

*(x+i)

which is of type char. It's an element of the array x you have declared. In order for this program to work properly you must change the declaration of the array to the following:

char* x[10]

The above declaration means that you want an array which can hold up to 10 elements of type char* (aka string).

  • Please be aware that an array of pointers to characters does not provide the space for the characters, only the pointers. Before copying, you need to provide enough memory for each array of characters. – the busybee Jul 29 '22 at 08:48