You haven't initialized the values in the a
array, so you can't use them. When you call strcpy
, you pass it the value of a[0][0]
as the address to copy data into -- but it has no particular values yet because you've not set it to any value. So you're asking strcpy
to copy the string to no place in particular.
You must assign a variable a value before you attempt to use its value. You must make a pointer point to something before you try to use the pointer's value as if it pointed to something.
You could do this:
a[i][0] = strdup (item);
The strdup
function allocates memory and copies a string into that memory, returning a pointer to it. After this, a[i][0]
will point to that newly-allocated memory that contains a copy of the string.
If your platform doesn't provide strdup
, you can easily code it:
char *strdup (const char * i)
{
char *j = malloc (strlen(i) + 1);
if (j == NULL)
return NULL;
strcpy (j, i);
return j;
}
You also access the array past the end. Your array has only five objects with indexes 0, 1, 2, 3, and 4. So using an index of 5 is also illegal.
Lastly, in your scanf
calls, you pass the address of your arrays of characters, like this: scanf("%s",&item);
. Since item
is an array, it will decay into the address of its first element, which is what you want. You don't want the address of the array, so the &
is incorrect.