我是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++;
}
你我的朋友,是一個救星!!!!!!! – user2158382 2013-04-20 23:03:53