Reputation: 91
I define a structure that I call Neuron
.
In my main, I create a two dimensional array:
Neuron N_network[4][10]; //create a neural network
I have a function to print the structure element:
void print_stat_neuron(Neuron * neuron_info)
What should I write to print N_network[i][j]
(as an argument of the print_stat_neuron()
function)?
I try simply with &(N_network[i][j])
and I get an error message.
I have the fallowing error message with the compiler gcc -Wall -Wextra:
2ex4.c: In function 'init_network':
2ex4.c:168:2: warning: implicit declaration of function'print_stat_neuron' [-Wimplicit-function-declaration]
print_stat_neuron(&N_network[4][1]);
^
2ex4.c: At top level:
2ex4.c:191:6: warning: conflicting types for 'print_stat_neuron' [enabled by default]
void print_stat_neuron(Neuron * neuron_info)
^
2ex4.c:168:2: note: previous implicit declaration of 'print_stat_neuron' was here
print_stat_neuron(&N_network[4][1]);
Upvotes: 1
Views: 61
Reputation: 753615
The error messages you show come from not declaring the function before using it, so the compiler deduces a type for the function (extern int print_stat_neuron();
with no specification for the number or types of the parameters), and then objects to you defining it with a return type of void
. Note that in C, extern int print_stat_neuron();
is not a prototype for a function with no arguments; that's extern int print_stat_neuron(void);
.
This code compiles cleanly:
typedef struct Neuron
{
int x;
} Neuron;
void print_stat_neuron(Neuron *neuron_info);
int main(void)
{
Neuron N_network[4][10];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 10; j++)
print_stat_neuron(&N_network[i][j]);
}
return 0;
}
This declares the function before using it. It would also be acceptable to define the function before using it (which saves having a separate declaration). Of course, if the function will be used outside the source file where it is defined, it should have a declaration in a header, and the header should be used both where it is defined and where it is used. If the function is not used outside the source file where it is defined, it should be static
.
Clean compilation checked with GCC 5.3.0 on Mac OS X 10.10.5 (my El Capitan machine is under warranty repair, unfortunately):
$ gcc -std=c11 -O3 -g -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes \
> -Wold-style-definition -c neuron.c
$
Upvotes: 2
Reputation: 106012
You need to change the parameter to Neuron (*neuron_info)[10]
. Now you can call this function as
print_stat_neuron(N_network);
or
print_stat_neuron(&N_network[0]);
Upvotes: 3