I am trying to fill a multidimensional array with chars.
The task is: I need to fill 30 names in array, each name could not be longer than 20.
char names_arr[NAME_BUF_SIZE][MAX_NAME_LENGTH];
So, I wrote such a method:
void read_from_input(char arr[][MAX_NAME_LENGTH])
{
unsigned r = 0;
int i = 0, j = 0;
char tmp[MAX_NAME_LENGTH];
for (i = 0; i < NAME_BUF_SIZE /*30*/; i++)
{
for (j = 0; j < MAX_NAME_LENGTH /*20*/; j++)
{
if((r = scanf("%s", tmp) != EOF) && (r != 0))
{
arr[i][j] = tmp;
}
}
}
}
Such a way I would like to fill my array with names.
But the issue is that I get a such warning
main.c: In function ‘read_from_input’:
main.c:31:27: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
arr[i][j] = tmp;
^
What am I doing wrong?
tmparray? Herearr[i][j] = tmp;you're trying yo assign the array to the element of second array.arr[i][j]is a char, and you are assigning to ittmpthat is a char array, that is actually a pointer to the first char of the array.'\0'.