Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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.

C
#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.
Posted
Updated 2-Oct-13 18:12pm
v2

1 solution

If you read this:

http://www.eskimo.com/~scs/cclass/notes/sx10f.html[^]

and this:

http://www.functionx.com/cpp/Lesson14.htm[^]

You will find this is how your code should read.

C++
#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.1f;
 
    ActivationFunc(x1, w, &n, d);
} 


Summary - When you pass an array to a function the compiler interprets it as a pointer. In sgnFunction you could even replace input[2] with *(input + 2).
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900