2017-02-22 66 views
0

我有一個名爲SemCreate的函數,它接受一個名稱爲int和一個指針作爲參數。我希望指針指向一個新的結構,我想返回一個int,如果它沒問題,則返回0。嘗試設置指向結構的指針

int P1_SemCreate(char *name, unsigned int value, P1_Semaphore *sem){ 
USLOSS_Console("Create a semaphore\n"); 
if(!verifyKernel()) { 
     //USLOSS_Console("ERROR: Not currently in Kernel mode\n"); 
     USLOSS_Halt(1); 
    } 
if(numSems>= P1_MAXSEM){ 
    //USLOSS_Console("ERROR: MAX semaphore already exist\n"); 
    return -2; 
} 
if(name == NULL){ 
    return -3; 
} 

interruptsOff(); 
int i; 
for (i = 0; i < P1_MAXSEM; i++) { 
    if (semTable[i].inUse == 0) { 
     semTable[i].value = value; 
     semTable[i].list = NULL; 
     semTable[i].name = malloc(sizeof(char)); 
     strcpy(semTable[i].name, name); 
     semTable[i].inUse = 1; 
     semaphore *temp = NULL; 
     temp = malloc(sizeof(semaphore)); 
     temp->value = value; 
     temp->list = NULL; 
     temp->name = malloc(sizeof(char)); 
     strcpy(temp->name, name); 
     *sem = temp; 

     break; 
    } 

} 
numSems++; 
interruptsOn(); 
return 0; 

}

眼下指針是函數內的罰款,但一旦我返回指針爲空

編輯:數組semTable是信號量的數組

typedef struct semaphore{ 
    int value; 
    char * name; 
    struct node *list; 
    int checkPoint; 
    int inUse; 
}semaphore; 


typedef struct PCB { 
    USLOSS_Context  context; 
    int     (*startFunc)(void *); /* Starting function */ 
    void     *startArg;    /* Arg to starting function */ 

    int   pid; 
    int   status;  
    int   killedStatus;  
    int   state; 
    int   priority; 
    char  name[50]; 
    int   parentPID; 
    int   numOfChild; 
    int blockFlag; 
    struct sempahore *blockSem; 
    char  *stack; 
    struct node *children; 
    struct node *zombiList; 
    int   cpuTime; 
    int   startTime; 
    struct semaphore *childSem; 
} PCB; 

typedef struct node { 
     PCB *pcb; 
     struct node *next; 
} Node; 
+2

我在函數體中看到的唯一結構是'semTable',但是不顯示它的創建位置。傳入函數參數包括'P1_Semaphore * sem',但是'sem'!='semTable'。修復你的代碼片段以包含所有相關的部分。 – ryyker

+1

你確定要爲'name'分配1個字節嗎?它只能容納一個角色。並且總是發佈[mcve] –

+0

什麼是'P1_Semaphore'?與信號量有什麼不同? –

回答

1

你的問題並不完全清楚你想要做什麼。所以,這個答案解決了以下一般性問題,希望他們能夠提供幫助:

1)通過函數傳遞結構體的地址,更改結構體成員的值以及訪問調用函數中更改後的值。 (它與您所展示的不一樣,但說明了您想要做什麼。)
2)創建實例和指向結構實例的指針,然後初始化。
3)包含指向自引用結構的教程鏈接。 (因爲你是在你的代碼的發佈做)

typedef struct {//struct definition created in global space, typedef'ed 
    char line[80]; //to read in the line 
    int slot; 
    char type[20]; //long enough for any types listed 
    int position; 
}SLOT; 
int modifyStruct(SLOT *slot);//prototype of demonstration function 

int main(void) 
{    
    SLOT s;//create instance of SLOT local to main 
    int ret = modifyStruct(&s);pass address of SLOT instance to change member values 
    printf("Line: %s\n", s.line);//show changed values 
    printf("Slot: %d\n", s.slot); 
    printf("type: %s\n", s.type); 
    printf("position: %s\n", s.position); 

    return 0; 
} 


int modifyStruct(SLOT *slot) 
{ 
    strcpy(slot->line, "lineA"); 
    slot->slot = 2; 
    strcpy(slot->type, "large"); 
    slot->position = 20; 

    return 0; 
} 

編輯 - 爲了解決意見,詢問如何設置一個結構指針指向一個結構的問題。

首先,它通過查看您發佈的代碼來顯示您正在使用自引用結構。 (即包含作爲其自身的指針實例的成員的結構)以下是鏈接到使用自引用結構的良好的tutorial dealing with Linked Lists in C

關於你的評論:_我想我應該說得更清楚。 P1_Semaphore與信號量不同。我需要P1_semaphore指向信號量。:

如果P1_Semaphoresemaphore不同,則不應將其設置爲指向另一個的地址。無論如何,你的編譯器不會讓你這樣做。

正如我在評論中所述,一個結構指針應該只指向內存中包含該結構實例的位置。例如,考慮兩個結構一個& B:

typedef struct { 
    int iNum; 
    float fNum; 
    char cStr[80]; 
}A; 

A a, *pA; //create an instance, and pointer to an instance of A 

typedef struct { 
    int iNum1; 
    int iNum2; 
    int iNum3; 
}B; 

B b, *pB; //create an instance, and pointer to an instance of B 

A & B有明顯的不同,並且會佔用不同的大小和形狀記憶,所以如果指針B*pB被設置爲指向任何東西,但B,這將是不正確的。它需要指向B

正確:
PA = &一個//設置指針到一個等於A
PB = & b //設定指針的地址到B等於B的地址

不正確:
PA = & b //設置指針到一個等於b的地址
PB = &一個//設置指針到b等於A
的地址(典型的編譯器錯誤 - =的操作數具有類型指針B和指針甲

+0

我想我應該說得更清楚。 P1_Semaphore與信號量不同。我需要P1_semaphore指向信號量 –

+0

@SeanGallagher - struct * pX'應該指向'struct Y'的唯一時間是它們是否是同一個結構體定義的實例。請參閱我的答案的底部以解釋此問題的編輯。 – ryyker

0

C/C++,所有的參數是按值傳遞,而不是通過引用。要獲取參數中的結構指針,您需要使用指針指針作爲參數。像這樣:

int P1_SemCreate(char *name, unsigned int value, P1_Semaphore **sem){ 
    ... 
     *sem = temp; 
    ... 
} 
+0

沒有「C/C++」。在C++中,一些參數通過引用傳遞。 –