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);
CodePudding user response:
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).
CodePudding user response:
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