function definition

Hi.
Is the following function defined correctly?

Thanks in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int ReadFile(file, size, graph)
 FILE* file;

 int* graph;
{
  int i, j;

  fscanf(file, "%d", &size);

  graph = (int*) malloc( size * size * sizeof(int));

  for (i = 0; i < size; i++)
    for (j = 0; j < size; j++)
      fscanf(file, "%d", &(graph)[i*size+j]);

  fclose(file);
  return(1);
}

I always thought that the variable must be defined inside the body in the header of the function
This is "old-style" C function declaration. It is allowed (but deprecated) in C and not allowed in C++.

Also, the type of size is missing: this was only allowed in C89 (which defaulted to int), and is forbidden in C since C99.
You mean all three arguments are int's?

How will the scope be defined?
Last edited on
hooshdar3 wrote:
You mean all three arguments are int's?
No, only the second argument, since it is the only one not specified.
hooshdar3 wrote:
How will the scope be defined?
How do you think? The scope is the scope of the function body.
You are Missing the type of Argument for your function.
Either your function should be

int ReadFile(FILE* file,int size, int * graph)

or


FILE* file;
int* graph;
{
int i, j;

fscanf(file, "%d", &size);

graph = (int*) malloc( size * size * sizeof(int));

for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
fscanf(file, "%d", &(graph)[i*size+j]);

fclose(file);
return(1);
}
int ReadFile(file, int size, graph)
@Rockyy: Did you read @Cubbi's post?
Topic archived. No new replies allowed.