简体   繁体   English

在函数中使用指针并修改main()

[英]Using pointers in functions and modifying main()

I would like to pass a pointer as an argument into a function that normalizes the vector pointer that was passed as an argument. 我想将指针作为参数传递给函数,该函数对作为参数传递的矢量指针进行规范化。

Here are the functions: 功能如下:

float norm(Vector *v) {
    float len;
    len = sqrt(pow(v->x, 2.0) + pow(v->y, 2.0) + pow(v->z, 2.0));
    return len;
}

Vector normalize(Vector *vec){
  Vector normVec;
  //calls norm function which also takes a pointer of vector type as argument
  float norm = norm(&vec);

  normVec.x = vec->x/norm;
  normVec.y = vec->y/norm;
  normVec.z = vec->z/norm;

  return normVec;
}

I get this error: error: called object 'norm' is not a function or function pointer. 我收到此错误:错误:被称为对象'norm'的不是函数或函数指针。
How can I get this function running smoothly? 如何使此功能平稳运行?

That is because your float norm variable shadows the norm() function. 那是因为您的float norm变量遮盖了norm()函数。
Name one of them something else. 给其中之一命名。


Also, vec is already a pointer, no need to take its address ( &vec ) when you pass it to norm() 另外, vec已经是一个指针,将其传递给norm()时无需获取其地址( &vec norm()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM