Where's the error? (C)

cquestion's Avatar
Go4Expert Member
Hello, i am doing a project and I have a problem with the compilation. It says me that before the penultimate " } " there's an error. Culd you help me to find it?
Code:
#include <stdio.h>
#include <string.h>

void leer(FILE *Elementos)
{
     Elementos=fopen("elementos.txt", "r");
     int i=0, j=0; char todos_fich[i][2];
     do
     {
       for(j=0;j=1;j++)
       {
         fscanf(Elementos, "%s", todos_fich[i][j]);
       }
      i++;
     
     }
     fclose(elementos.txt);
}

Last edited by shabbir; 9Dec2007 at 12:03.. Reason: Code block
0
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
Where is the 'while' which matches your 'do' ?

Also, use [code][/code] tags when posting code.
0
cquestion's Avatar
Go4Expert Member
Hello Salemn. Ok, i understand the problem. But i have a new one about it: i want to fill todos_fich with the character strings in elementos.txt. So what should i include in while ( )?. I have tried while (fscanf(Elementos, "%s", todos_fich[i][j]) != EOF), but it returns error...
0
cquestion's Avatar
Go4Expert Member
I'm sorry, I fortot using [code]: the thing I write (and doesn't work) is
[code] while (fscanf(Elementos, "%s", todos_fich[i][j]) !=EOF);
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
I have added the code block for you and you should start before the code with [code] and end it with [ /code]
0
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
How do you know how many lines/words there are in the file?

> char todos_fich[i][2];
Aside from the fact that i is zero at this point, this doesn't create an array you can just extend at will.

> for(j=0;j=1;j++)
Refer to your book for proper syntax. Say
for(j=0;j<2;j++)

> fclose(elementos.txt);
This takes the file handle, Elementos as the parameter.

Passing Elementos as a parameter does no good. You'll get the same effect by making it a local variable.
0
cquestion's Avatar
Go4Expert Member
Hello Salem/shabbir , the problem is that I haven't text book for study. I am Spanish and the only book I have been told to buy is the one writed by "Kernich"(may be wrong writed, I don't remember the exact name). But that book isn't in Spanish language, and I have considered ordering it in English, but I'm afraid of missunderstand some words leading me to important missconceptions.

Another think I'm interested in is that, as far as I know, matrixes are declared as, for example a matrix of inttegers:
Code:
int mat[10][20]
; But I have some problems linking that way of declarations with the pointers way. I thing that
Code:
 int *p=&mat
means
Code:
p=&mat[0]=&mat
. But if am referring to the pointer which contents the adress of
Code:
*p=&mat[0]
, what kind of pointer is? I mean , (kind of pointer)
Code:
*(*p)
. Also I have a second question: I think I could 'make' a matrix using those pointers, but I think that a matrix and a pointer to pointer of (kind of data) aren't the same thing, unlike what people and internet manuals usually say, because when I declare the matrix, I save a size of memory for my matrix, but if I do that with pointers...................¿Could their adress get mixed with other one's and give me problems?
Note: The only solve I find is doing malloc when declaring the pointer. If I do that, can I assure that those pointer to pointers are exactly the same than matrixes?

Thanks a lot, you help me so much...
0
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
I recently posted how to allocate a 2D array here
http://www.go4expert.com/showthread.php?t=7759