Hmm, I was trying to load strings from a file(formated) to an 2D Array... but....

FILE * fp;
char TempBuffer[100][100];
char array_game[100][100];
fp=fopen(filename,"r");
for(i=0; i<100; i++)		// Create 2D array to Hold the array of the file
{	
	for(j=0; j<100; j++)					
	{
			fgets(TempBuffer,101,fp);		
			strcpy(&array_game[i][j],TempBuffer);		
	}
}

I get these warnings:

warning: passing argument 1 of ‘fgets’ from incompatible pointer type
: note: expected ‘char * __restrict__’ but argument is of type ‘char (*)[100]’

warning: passing argument 2 of ‘strcpy’ from incompatible pointer type
/usr/include/string.h:127: note: expected ‘const char * __restrict__’ but argument is of type ‘char (*)[100]’

Please, help I'm stuck in this point some hours!
xxx

Edited 6 Years Ago by Wizablue: n/a

You appear to be trying to use TempBuffer to temporarily read a string before storing it. If you are reading a string from a file into a buffer how many dimensions does the buffer need?

When calling fgets it is a really bad idea to give the buffer size as bigger than the size of the buffer you are actually passing a pointer to.

How many line does this file contain? How many times does your code call fgets?

You are not checking the return value of fgets so you will continue trying to process the file even if an error occurs or the program gets to the end of the file.

Is array supposed to be an array of 100 strings or an array of an array of 100 strings (or colloquially are 2 dimensioned array of strings with both dimensions sized as 100)?

You appear to be trying to use TempBuffer to temporarily read a string before storing it. If you are reading a string from a file into a buffer how many dimensions does the buffer need?

i thought the same as the array, in this example 100*100

When calling fgets it is a really bad idea to give the buffer size as bigger than the size of the buffer you are actually passing a pointer to.
How many line does this file contain? How many times does your code call fgets?

the file has 100 lines and 100 columns (it's formatted to be an array)

You are not checking the return value of fgets so you will continue trying to process the file even if an error occurs or the program gets to the end of the file.

Is array supposed to be an array of 100 strings or an array of an array of 100 strings (or colloquially are 2 dimensioned array of strings with both dimensions sized as 100)?

No,this is just a short part of the code. I have an counter until it gets EOF.
It is supposed to be a 2D array 100*100.

Hmm, I was trying to load strings from a file(formated) to an 2D Array... but....

int j;               //new
FILE * fp;
char TempBuffer[101];
char array_game[100][100];
fp=fopen(filename,"r");
for(i=0; i<100; i++)		// Create 2D array to Hold the array of the file
{	
	for(j=0; j<100; j++)					
	{
			fgets(TempBuffer,100,fp);		
                        TempBuffer[100] = '\0';
			strcpy(&array_game[i][j],TempBuffer);		
                        TempBuffer[0] = '\0';
	}
}

That will remove the newline char from the end of TempBuffer, and give you a bit of defensive programming, as well.

The char's that fgets() can have at the end ('\n' and '\0'), can be a problem.

Thanx Adak! You really saved me! I couldnt see this so many hours!
Actually, I tried with the same way as u proposed, to do it for an 1D array. But for the following code i get this warning:

error: subscripted value is neither array nor pointer

strcpy(&array_words[i][0],TempBuffer2);

Somewhat strange as I dont get the same warning for the 2D array.
Thanx in advance

oops my fault! the array doesnt need a second value
the correct:

strcpy(&array_words[i],TempBuffer2);

sorry i feel fool

Edited 6 Years Ago by Wizablue: n/a

This question has already been answered. Start a new discussion instead.