c - 定义函数并传递结构值

标签 c function struct declare

我是 C 新手,有几个关于将结构体值放入函数以及声明函数的正确方法的问题。

在我的 common.h 中我定义了

extern struct ddrautocal;
int get_eeprom_vals(uchar); // is this the correct declare the function?

在calibration.c中,我定义了我的结构并更改了一些设置的一些值(未显示)

#include <common.h>
struct ddrautocal {
    u32 wdtr;
    u32 clkp;
    u32 rdcc;
};

在 proc.c 中

#include <common.h>
int get_eeprom_vals(struct ddrautocal *cal){
// I'd like to access cal.wdtr and cal.clkp
}

我是一个彻头彻尾的傻瓜,我知道,但我正在努力变得更好。我一整天都在努力让它工作,想知道我是否在 common.h 中正确声明了该函数,以及访问位于 proc.c 中的 get_eeprom_vals 函数中的 ddraautocal 结构的正确方法是什么?任何帮助都会不胜感激。谢谢!

最佳答案

int get_eeprom_vals(uchar);签名错误..应该是:

int get_eeprom_vals(struct ddrautocal *cal);因此前向声明和函数定义的签名匹配。

要访问成员,您需要:cal->wdtr , cal->clkp(*cal).wdtr (*cal).clkp

-> 使用指针间接运算符。 * 是间接运算符和句点(点)的组合,用于访问该字段。

我还认为你需要 #include "common.h"而不是#include <common.h>因为 common.h 不是系统头。

关于c - 定义函数并传递结构值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13148204/

相关文章:

c - 为什么我无法打印存储在创建的数组中的值?

javascript - react 。创建一个返回 html 的函数

python - 如何从递归 Python 函数返回值?

c - 使用 getline() 时出现打印问题

python - 如何使用Cython将Python 3编译成C

c - pthread总线错误

c - 尝试存储指针数组时出现类型不兼容错误

java - Servlet 发送请求后未正确响应

c - C 中的多重链表

c - 未定义类型的无效使用 ‘struct book_implementation’