2012-04-25 70 views
18

enter image description here無法解決此問題.. 我正在實施隊列。編寫完整的代碼後我有下列錯誤:'''''',';','asm'或'__attribute__'在''之前。代幣

expected '=', ',', ';', 'asm' or '__attribute__' before '.' token

然後我寫了一個簡單的程序,但同樣的問題仍然存在。無法理解如何解決這個問題。我已經在很多方面研究了stackoverflow.com and google.com的解決方案,但仍無法解決此問題。請幫助。

我想initialize globallyQ.front = Q.rear = Any value

#include <stdio.h> 
#include <stdlib.h> 
struct Queue 
{ 
    int front, rear; 
    int queue[10] ; 
}; 
struct Queue Q; 
Q.front = 0; 
Q.rear = 0; 

int main() 
{ 
    return 0; 
} 
+0

非常感謝球員。我得到了答案。這就像我吸取的教訓。無論如何,你們是最棒的。 – 2012-04-25 07:20:20

回答

12

Q.front = 0;不是一個簡單的初始值設定項,它是可執行的代碼;它不能發生在函數之外。使用適當的初始化程序Q

struct Queue Q = {0, 0}; 

或命名初始化語法(在所有的編譯器只在C不可用,並且尚未):

struct Queue Q = {.front = 0, .rear = 0}; 
+0

雅,我理解這個概念。謝謝拍賣 – 2012-04-25 07:20:50

+0

謝謝它幫助我 – achoora 2016-07-14 12:27:50

5

您可以在全球範圍內使用Q.front = 0; Q.rear = 0;沒有初始化的變量。這些陳述應該在main之內。

3

正如@Naveen說你不能分配到結構的成員是在全球範圍內。根據C的版本,但你可以這樣做:

struct Queue q = {0,0}; 

struct Queue q = {.front = 0, .rear = 0 };