Reputation: 670
I'm having some trouble working with passing around arrays of floats. I throw some arrays of floats into the ActivationFunc, and then from there I throw those same arrays into the sgnFunction, which for some reason ends up having different values.
#include <stdio.h>
void sgnFunction(float *input[], float *weight[])
{
printf("VALUES: %.2f %.2f %2.f, WEIGHTS: %.2f, %.2f, %.2f\n", *input[0], *input[1], *input[2], *weight[0], *weight[1], *weight[2]);
}
void ActivationFunc(float *x, float *w, float *n, int *d)
{
printf("VALUES: %.2f %.2f %2.f, WEIGHTS: %.2f, %.2f, %.2f\n", x[0], x[1], x[2], w[0], w[1], w[2]);
sgnFunction(&x, &w);
}
int main()
{
float x1[3] = {1, 0, 1};
float x2[3] = {0, -1, -1};
float x3[3] = {-1, -0.5, -1};
int d[3] = {0, 1, 1};
float w[3] = {1, -1, 0};
float n = 0.1;
ActivationFunc(x1, w, &n, &d[0]);
}
If I remove the '&' from "sgnFunction(&x, &w);", I get a compiler error of:
test.c: In function 'ActivationFunc':
test.c:10:9: warning: passing argument 1 of 'sgnFunction' from incompatible pointer type
test.c:2:14: note: expected 'float **' but argument is of type 'float *'
Which I don't understand what it means to fix it. I know I'm probably just screwing something up with my use of pointers. A nice explanation of what's wrong, what I'm doing wrong with my pointers, and how to fix this would be greatly appreciated.
Upvotes: 1
Views: 249
Reputation: 411
The key is, in c, the array subscript operator has higher precedence than the de-reference operator. Hence the expression *input[0] will be evaluated as *(input[0]).
consider the declaration,
int a[3][4] = { {1, 2 ,3}, {4, 5, 6} };
Here 'a' is a 2-D array, '&a[0][0]' projects the address of the first element, 'a[0]' projects the address of the first row and 'a' projects the base address of the array. If you print the address in all three cases mentioned above you will get the same value, but the nature of address is different. In case 1, the address refers to an element, in case 2, the address refers to a row of elements, In case 3, the address refers to the entire array.
In your function, sgnFunction,
while evaluating, *input[0], it is evaluated as *(input[0]) this refers to first row of 'input' which is 'x' so your value is as expected. But while evaluating input[1] ((input[1])) this refers to second row of 'input'. But there is no second row. Hence the garbage value. To fix this, change the *input[1] to (*input)[1].
On a side note, While calling the function sgnFunction, it should be sufficient to use call-by-value instead of call-by-reference.
Upvotes: 0
Reputation: 61
void sgnFunction(float *input[], float *weight[])
Don't you mean: void sgnFunction(float *input, float *weight)
float *a[] is effectively a float **a
Upvotes: 0
Reputation: 10516
if you call function like this
sgnFunction(x, w);
Your definition should be
void sgnFunction(float *input, float *weight) // you just need to change array of pointers to single pointer
{
printf("VALUES: %.2f %.2f %2.f, WEIGHTS: %.2f, %.2f, %.2f\n", input[0], input[1], input[2], weight[0], weight[1], weight[2]); // here also
}
Upvotes: 2
Reputation: 34625
sgnFunction(&x, &w);
The problem is that you are passing address of pointer which is of type float **
. Just do -
sgnFunction(x, w);
x
is of type float *
. So, doing &x
will yield the address of a pointer which is of type float **
.
Also, passing an array will decay to the pointer to the first element in the array. So change the function signature to -
void sgnFunction(float *input, float *weight);
Upvotes: 0