2013-04-20 39 views
1

我是C新手,我不完全理解所有這些指針和內存分配的東西,所以對不起,如果我在概念上是錯誤的。我正在嘗試訪問字符串數組中的字符串元素,但字符串數組位於結構中,並且每次嘗試訪問它時,程序都會崩潰。C:無法從結構中的字符串數組訪問元素

我得到一個錯誤,當我嘗試這樣做if語句檢查

if (strcmp(functionList[holder].otherServers[i], "") == 0) 

我只是想檢查,如果結構的數組中的當前結構元素(functionList [持有者])有一個空值在其字符串數組中填充其元素(otherServers [i])。當它找到它的第一個空元素時,我想要做的就是複製字符串數組索引中的字符串(otherServers [i])

這裏是我的代碼(注意:我拿出了很多我以爲代碼爲無關的問題)

struct function { 
    char name[20]; 
    int parameterNumer; 
    int canDo; 
    //currently the system has a 10 server max. You can change this easily 
    char *otherServers[10];  
}; 

//global scope variables 
//currently the system has a 10 server max. You can change this easily 
char *serverList[10]; 
struct function functionList[10] = {{"",0, 0, {}}}; 
int numberofOtherServers; 

while(strcmp(functionList[i].name, "") != 0 && i != -1) 
{    
    //if the function exist in the functionList already, then just add server to the functions list of capable servers 
    if(strcmp(functionList[i].name, functionName) == 0 && functionList[i].parameterNumer == functionParam) 
    { 
     holder = i; 
     //function found so go through the functions list of servers and add it to the list 
     i = 0; 
     while(i >= 0) 
     { 
      if(strcmp(functionList[holder].otherServers[i], "") == 0) 
      { 
       strcpy(functionList[holder].otherServers[i], serverHelloName); 
       i = -1; // 
      } 
      if(i == 9) 
      { //ran through entire list of all possible servers and couldnt find an empty slot 
       printf("server list full, should allow more room for other servers"); 
       fflush(stdout); 
       i = -1; 
      } 
     } 
     printf("yay"); 
     fflush(stdout); 
    } 
    if(i == 9) 
    { //ran through entire list of all possible functions and did not see an empty slot or there is no match 
     printf("function list full so could not add, and there was no match for any functions"); 
     fflush(stdout); 
     i = -1; 
    } 
    i++; 
} 

回答

1

您的代碼不顯示的otherServers分配。如果您有一組字符指針(如otherServers),則需要爲每個字符串分配內存,以便有一些指向的內容。

這意味着你需要檢查指針指向有效的地方,你可以做到這一點strcmp()strcpy()前:

if(strcmp(functionList[holder].otherServers[i], "") == 0) { 
    strcpy(functionList[holder].otherServers[i], serverHelloName); 
    i = -1; 
} 

而是這個片段將檢查otherServers[i]尚未分配,然後分配足夠的內存來存儲字符串:

if (functionList[holder].otherServers[i] == NULL) { 
    // add one for the terminator 
    functionList[holder].otherServers[i] = malloc(strlen(serverHelloName) + 1); 

    // make sure the allocation worked 
    if (functionList[holder].otherServers[i] == NULL) { 
     // something went wrong so bail 
     break; 
    } 
    strcpy(functionList[holder].otherServers[i], serverHelloName); 
} 

當你與otherServers[]functionList[]本身完成後,你需要釋放內存早先分配:

for (i = 0; i < 10; i++) { 

    if (functionList[holder].otherServers[i] != NULL) { 
     free(functionList[holder].otherServers[i]); 
     functionList[holder].otherServers[i] = NULL; 
    } 
} 
+0

你我的朋友,是一個救星!!!!!!! – user2158382 2013-04-20 23:03:53

1

這是更好地把NUL代替普通的「」在一個初始化:

struct function functionList[10] = {{{'\0'},0, 0, {}}}; 

要檢查你的榜樣名稱是否然後將其分配或沒有,你只是取消對它的引用和檢查的NULL字符:

*functionList[i].name == '\0' 

strcmp檢查一個空字符(又名零終止),在偏移開始供應,並會繼續下去的陣列超越,如果它沒有找到一個 - R的導致未定義的行爲,很可能是訪問衝突,這取決於如何分配此緩衝區。

SpacedMonkey擊敗了我到剩下的有效答案;你需要爲一個字符串分配存儲空間。默認情況下,指針指向內存中的某個區域 - 在使用前必須用malloc手動分配它,然後使用free釋放它。