我正在尝试使用struct实现一个简单的环形缓冲区。我之前从未使用过结构,只是开始尝试理解它们。编译器退出时出现此错误:
expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
错误指的是最后两行。
#define MAX_PROCESSES 16
struct ring_buffer{
uint8_t data[MAX_PROCESSES];
uint8_t size;
uint8_t count;
uint8_t write_pos;
uint8_t read_pos;
};
struct ring_buffer ring_buffer_processes;
ring_buffer_processes.size = MAX_PROCESSES;
ring_buffer_processes->size = MAX_PROCESSES;
我使用to运算符只是为了测试目的,看看它是否有效。这当然应该只有一行。
我也尝试过使用“typedef”,但这也无济于事。这有什么不对?
由于
答案 0 :(得分:3)
您的代码有两个错误:
因此纠正这些要点会给我们:
#include <stdint.h>
#define MAX_PROCESSES 16
struct ring_buffer{
uint8_t data[MAX_PROCESSES];
uint8_t size;
uint8_t count;
uint8_t write_pos;
uint8_t read_pos;
};
int main(void)
{
struct ring_buffer ring_buffer_processes;
struct ring_buffer * p_ring_buffer_processes;
p_ring_buffer_processes = &ring_buffer_processes;
ring_buffer_processes.size = MAX_PROCESSES;
p_ring_buffer_processes->size = MAX_PROCESSES;
return 0;
}
答案 1 :(得分:1)
要访问结构的任何成员,我们使用成员访问运算符(。)但
要使用指向该结构的指针访问结构的成员,必须使用→
运算符。
更改代码..
#define MAX_PROCESSES 16
struct ring_buffer{
uint8_t data[MAX_PROCESSES];
uint8_t size;
uint8_t count;
uint8_t write_pos;
uint8_t read_pos;
};
struct ring_buffer ring_buffer_processes;
void main(){
ring_buffer_processes.size = MAX_PROCESSES;
//ring_buffer_processes->size = MAX_PROCESSES; Not required you can access struct variable with . operator.
}
答案 2 :(得分:0)
在C语言中,非指针对象已使用进行访问。运算符和A指针已使用 - &gt; 运算符访问对象。
#define MAX_PROCESSES 16
struct ring_buffer{
uint8_t data[MAX_PROCESSES];
uint8_t size;
uint8_t count;
uint8_t write_pos;
uint8_t read_pos;
};
struct ring_buffer ring_buffer_processes;
ring_buffer_processes. size = MAX_PROCESSES;
struct ring_buffer *ptr_ring_buffer_processes;
*ptr_ring_buffer_processes-> size =& MAX_PROCESSES;