访问结构体中的指针变量

发布于 2024-11-06 21:19:25 字数 193 浏览 1 评论 0原文

#define SIZE 9

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

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

#define SIZE 9

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

Can anybody tell me how to access "tail" & "head"? Using the variable gsm (gsm should be used as struct variable not as a pointer).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有影子陪我不离不弃 2024-11-13 21:19:25
#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;
}
#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;
}
沉溺在你眼里的海 2024-11-13 21:19:25
gsm.tail[INDEX]

*(gsm.tail)


int main(int argc, char **argv)
{
     #define SIZE 9

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

     strcpy(gsm.buff, "ohaiohai");
    gsm.tail = gsm.buff;
     gsm.head = gsm.buff;

     printf("%s\n", gsm.buff);
     printf("%s\n", gsm.tail);
     printf("%s\n", gsm.head);

     putchar(*(gsm.tail));
     putchar(gsm.head[1]);

     exit(0);
}

输出:

 $ gcc main.c && ./a.out
ohaiohai
ohaiohai
ohaiohai
oh
gsm.tail[INDEX]

or

*(gsm.tail)


int main(int argc, char **argv)
{
     #define SIZE 9

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

     strcpy(gsm.buff, "ohaiohai");
    gsm.tail = gsm.buff;
     gsm.head = gsm.buff;

     printf("%s\n", gsm.buff);
     printf("%s\n", gsm.tail);
     printf("%s\n", gsm.head);

     putchar(*(gsm.tail));
     putchar(gsm.head[1]);

     exit(0);
}

output:

 $ gcc main.c && ./a.out
ohaiohai
ohaiohai
ohaiohai
oh
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文