我是 C 的新手,我并不完全理解所有这些指针和内存分配的东西,如果我在概念上是错误的,很抱歉。我正在尝试访问字符串数组中的字符串元素,但字符串数组位于结构中,每次我尝试访问它时,我的程序都会崩溃。
当我尝试执行此 if 语句检查时出现错误
if (strcmp(functionList[holder].otherServers[i], "") == 0)
我只想检查结构数组(functionList [holder])中的当前结构元素是否为其字符串数组(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++;
}