C,编译器错误和指针 [英] C, Compiler-Error and Pointers

查看:79
本文介绍了C,编译器错误和指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理浮点数组时遇到了一些麻烦。我将一些浮点数放入ActivationFunc,然后从那里我将这些相同的数组抛入sgnFunction,由于某种原因,它最终会有不同的值。



< pre lang =c> #include < < span class =code-leadattribute> stdio.h >
void sgnFunction( float * input [], float * weight [])
{
printf( VALUES:%。2f%.2f%2.f,WEIGHTS:%。 2f,%。2f,%。2f \ n,* input [ 0 ],* input [ 1 ],*输入[ 2 ],* weight [ 0 ],*重量[ 1 ],* weight [ 2 ]);
}

void ActivationFunc( float * x, float * w, float * n, int * d)
{
printf( VALUES:%。2f%。2f%2.f ,重量:%。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 ]);
}





如果我从sgnFunction(& x,& w)中删除'&'; ,我得到一个编译器错误:



test.c:在函数'ActivationFunc'中:

test.c:10:9:警告:从不兼容的指针类型传递'sgnFunction'的参数1

test.c:2:14:注意:预期'float **'但参数类型为'float *'



我不明白解决它的意义。我知道我可能只是因为使用了指针而搞砸了。一个很好的解释是什么错,我的指针做错了,以及如何解决这个问题将非常感激。

解决方案

如果你读到这个:< br $> b $ b

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



和这个:



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



你会发现这就是你的代码应该阅读。



  #include   <   stdio.h  >  

void sgnFunction( float * input, float * weight)
{
printf( VALUES :%。2f%.2f%2.f,WEIGHTS:%。2f,%。2f,%。2f \ n,输入[ 0 ],输入[ 1 ],输入[ 2 ],权重[ 0 ],权重[ 1 ],权重[ 2 ]);
}

void ActivationFunc( float * x, float * w, float * n, int * d)
{
printf( VALUES:%。2f%。2f%2.f ,重量:%。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);
}





摘要 - 当您将数组传递给函数时,编译器会将其解释为指针。在sgnFunction中,您甚至可以用*(输入+ 2)替换输入[2]。


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.

解决方案

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.

#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).


这篇关于C,编译器错误和指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆