简体   繁体   中英

expected 'char * restrict' but argument is of type 'char' ,char * __cdecl strcpy(char * __restrict__ _Dest,const char * __restrict__ _Source); in C

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);

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).

It was so easy. I just had to declare a two dimensional array like x[10][10] for both individual characters and whole strings. I mean there might be better ways to do it but i'm still reading about memory allocation and stuff like that. Thanks Darth-CodeX and manos makris

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM