2
#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).

2 Answers 2

4
#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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1: for a good answer and a good guess at the underlying purpose of the struct
@Paul R: Reminds me way to much of the keyboard buffer back in my DOS days :)
-1
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

2 Comments

That's an improvement - -1 removed
Maybe I should use more explanation next time :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.