[英]I have a function that's returning a char* member of a struct. I am trying to call that function and assign a string to it, but am getting an error
[英]I am trying to use pointers to change the values in a struct via a function, but I get an error when inputting adress the in the function
我有一个程序,我首先在 inluded.h 文件中创建一个 typedef 结构,如下所示
typedef struct {
uint16_t x, y;
} vector_t;
然后我根据上面的定义在我的 main 中创建一个结构,就像这样
vector_t vec = {5,10};
然后尝试在下面的function中使用
void initVector(vector_t *v) {
(*v).x = 10;
(*v).y = 20;
}
我输入我的 function 以打印这样的语句。
printf("%d %d\n innit \n",vec.x,vec.y);
void initVector(&vec);
printf("%d %d\n \n",vec.x,vec.y);
但是,当我尝试构建程序时,出现以下错误
expected declaration specifiers or '...' before '&' token
当 function 被注释掉时,打印语句给出了向量,所以我认为这不是问题,但我不明白为什么它不应该工作。
任何帮助,将不胜感激
你想要的是调用 function initVector,而不是声明它,所以你必须替换
void initVector(&vec);
只需
initVector(&vec);
顺便说一句,在 function initVector 中,您可以编写:
void initVector(vector_t *v)
{
v->x = 10;
v->y = 20;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.