C:不能从结构中的字符串数组访问元素

时间:2013-04-20 21:59:29

标签: c arrays pointers struct

我是C的新手并且我并不完全理解所有这些指针和内存分配的内容,所以如果我在概念上错了,那就很抱歉。我试图访问字符串数组中的字符串元素,但字符串数组位于结构中,每次我尝试访问它时,我的程序崩溃。

当我尝试执行此语句检查

时,我收到错误
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++;
}

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:1)

最好在初始值设定项中放置一个NUL代替普通的“

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

要检查示例中的名称是否已分配,您只需取消引用它并检查NUL字符:

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

strcmp检查一个空字符(也称为零终结符),从提供的偏移量开始,如果找不到,则将继续超出数组 - 导致未定义的行为,很可能是一个访问违规,取决于此缓冲区的分配方式。

SpacedMonkey将我击败了有效答案的剩余部分;你需要为字符串分配存储空间。默认情况下,指针只指向内存中的某个区域 - 您必须先使用malloc手动分配它,然后再使用free