2016-11-25 28 views
0
#include<stdio.h> 
#include<conio.h> 
#include<stdlib.h> 

typedef struct PROCESS{ 
     int priority; 
     int lifecycle; 
     int ttl; 

}process1,process2,process3,process4,process5,process6; 

main(){ 
     PROCESS *waiting_queue; 
     waiting_queue = process1;  //this is were I get the error. 
     waiting_queue =(PROCESS *)malloc(6*sizeof(PROCESS)); 
     if(!waiting_queue){printf("no memory for waiting queue "); exit(0);} 


     getch();  
} 

我想創建一個結構數組與指針。我收到錯誤。 ';'之前的預期主要表達令牌';'之前的預期主要表達式代幣

+0

你是個*類型的聲明*和* *結構混合起來相當破碎方式。 AFAICT,你得到錯誤的行中的'process1'是一個* type *,而不是一個結構,使得這個語句有些違法。 – DevSolar

+0

[不要在C中輸入'malloc'的結果](http://stackoverflow.com/q/605845/995714) –

回答

4

你應該從(process1到process6)創建你的結構對象。

讓我給你舉個例子:

#include <stdio.h> 
#include <string.h> 

typedef struct student 
{ 
    int id; 
    char name[20]; 
    float percentage; 
} status; 

int main() 
{ 
    status record; 
    record.id=1; 
    strcpy(record.name, "Orcun"); 
    record.percentage = 86.5; 
    printf(" Id is: %d \n", record.id); 
    printf(" Name is: %s \n", record.name); 
    printf(" Percentage is: %f \n", record.percentage); 
    return 0; 
} 

這就是爲什麼你得到你的錯誤在你的主要功能。所以,你應該創建結構對象,如:

process1 processOrcun; 

您也可以點擊這裏:https://www.codingunit.com/c-tutorial-structures-unions-typedef

+0

謝謝你prometheus! – mfd

3

您有多個問題,而是一個使你的錯誤是,你沒有定義PROCESS,但結構使用該名稱。

當您使用typedef定義一個結構類型,類型名稱來自結構後:

typedef struct 
{ 
    ... 
} PROCESS; 

你有這個錯誤是因爲你定義如process1作爲類型,所以賦值(使指針指向一個類型)沒有任何意義。


另一個無關的問題是如何定義main函數。它必須被定義爲返回int(即使你的聲明中隱含,這是很好的做到這一點明確的),並進行控制,讓void的參數或者是一個整數和指針數組char。在你的情況下,它應該看起來像

int main(void) { 
    ... 
    return 0; 
} 
+0

謝謝我的朋友;我按照你的指示做了,但是給出了同樣的錯誤。 – mfd

+0

@mfd'waiting_queue'類型是什麼? 'process1'的類型是什麼?這些類型是否兼容(答案是否定的)?所以不,你不會得到*相同的*錯誤,但不同的。此外,該分配是無用的,因爲您直接重新分配'waiting_queue'。 –

+0

他們的類型是PROCESS。你的想法激發了我,我找到了解決辦法; waiting_queue =&process1;謝謝。 – mfd