gpt4 book ai didi

c - 访问结构中的指针变量

转载 作者:行者123 更新时间:2023-12-04 06:26:37 24 4
gpt4 key购买 nike

#define SIZE 9

struct circ_buff{
char buff[SIZE];
int total = 0;
char *tail;
char *head;
} gsm;

谁能告诉我如何访问“tail”和“head”?使用变量 gsm(gsm 应该用作结构变量而不是指针)。

最佳答案

#define SIZE 9
struct circ_buff{
char buff[SIZE];
int total; /* you can't initialize this here */
char *tail;
char *head;
} gsm;

int main() {
gsm.total = 0;
/* it looks like you're writing a circular buffer, so... set head/tail to the
* start of the buffer
*/
gsm.tail = gsm.buff;
gsm.head = gsm.buff;

/*
* gsm.head++; // increment as you add to the buffer, don't
* // forget to check for overflows
*
* // Other stuff you might want to do (assuming correct boundary checking)
*
* *gsm.head = 'G'; // set current head to 'G'
*
* printf("%c\n", *gsm.head); // print current value of head
*
*/
return 0;
}

关于c - 访问结构中的指针变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5975489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com