2011-05-12 6 views
2
#define SIZE 9 

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

"tail"& "head"에 액세스하는 방법을 알려줄 수 있습니까? 변수 gsm 사용 (gsm은 포인터가 아닌 struct 변수로 사용되어야 함).구조의 포인터 변수에 액세스

답변

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; 
} 
+0

+1 : 좋은 답변과 stru의 기본 목적에 대한 좋은 추측을 위해 ct –

+0

@Paul R : 내 DOS 시대에 키보드 버퍼의 많은 부분에 나를 생각 나게합니다 :) – forsvarir

-1
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 
+0

은 개선의 나의 친애하는 친구 – DipSwitch

+0

업데이트를 참조하십시오 - -1 어쩌면 좀 더 설명이 다음에 시간을 사용한다 –

+0

제거를 : D – DipSwitch

관련 문제