简体   繁体   English

strcmp分割错误?

[英]segmentation fault with strcmp?

I am trying to understand why my code crashes. 我试图理解为什么我的代码崩溃。 I have an array of structs which look like this: 我有一个看起来像这样的结构数组:

typedef struct contact {

    char cFirstName[10];
    char cLastName[10];
    char cTelphone[12];

} address ; // end type

In the code I initialize the array like this: 在代码中,我像这样初始化数组:

address myContacts[5];

for ( i = 0; i < 5 ; i++ ){
        strcpy(myContacts[i].cFirstName, "0");
        strcpy(myContacts[i].cLastName,"0");
        strcpy(myContacts[i].cTelphone,"0"); 
    }

This works: 这有效:

for ( i = 0; strcmp(myContacts[i].cFirstName,"0") != 0 ; i++ ){                                             
        printf("\nmyContacts[%d].cFirstName: %s", i, \
        myContacts[i].cFirstName );
    }// end for

So, I only print out Contacts which have content. 因此,我只打印出具有内容的联系人。

However, I can't under stand why my search contact function does not work: 但是,我无法理解为什么我的搜索联系功能不起作用:

void searchContact( address * myContacts,    char * name ){
    int found = 1;
    int i = 0;

    for ( i = 1; found != 0 ;i++ ){
    found=strcmp(myContacts[i-1].cFirstName, name);

    printf(" Name Found %s",   myContacts[i-1].cFirstName);
    }
} // end of searchContacts

I call this function like this: 我这样称呼这个功能:

printf("\nEnter a name or part of a name to search:\n");
            fscanf(stdin, "%s", buffer);
            getchar(); // clear the last enter
            printf("\nThe line you entered was:\n");
            printf("%s\n", buffer);
            searchContact( myContacts, buffer );

If I search for an existing name it is found, and everything is OK. 如果我搜索现有名称,则找到它,一切正常。 However, searching for a non existing name, causes a segmentation fault. 但是,搜索不存在的名称会导致分段错误。 Is there an obvious thing I am missing here ? 我在这里缺少明显的东西吗?

The probelm is here: 探针在这里:

        for ( i = 1; found != 0 ;i++ ){
        found=strcmp(myContacts[i-1].cFirstName, name);

        printf(" Name Found %s",   myContacts[i-1].cFirstName);
        }

You need to add something like for ( i = 1; found != 0 && i < num_of_contacts ;i++ ) otherwise you going out of your array! 您需要添加类似for ( i = 1; found != 0 && i < num_of_contacts ;i++ )否则您将退出数组!

Yes there is: you cycle past the end of the array. 是的,这里存在:您循环经过数组的末尾。 Your loops are not bounded at all. 您的循环完全不受限制。

You should limit looping on myContacts to the number of values it actually holds. 您应该将myContacts上的循环限制为它实际持有的值的数量。

如果搜索找不到任何结果,您将永远不会结束循环

The problem is in here: 问题在这里:

for ( i = 1; found != 0 ;i++ ) {
    found=strcmp(myContacts[i-1].cFirstName, name);
}

If you don't find name , the loop continues beyond the end of the array. 如果找不到name ,则循环将继续到数组末尾。 You need to add an extra test to your for loop to make it terminate if it reaches the end of the array without having found a match. 您需要在for循环中添加一个额外的测试,以使其在到达数组末尾而未找到匹配项的情况下终止。

As it happens, I don't understand why your for loop starts at 1 . 碰巧的是,我不明白为什么您的for循环从1开始。 It would be more natural to do it like this: 这样做会更自然:

for (i=0; found!=0 && i<5; i++) {
    found = strcmp(myContacts[i].cFirstName, name);
}

Also, your found variable feels poorly named. 另外,您found变量的名称不好。 It should perhaps be called notfound since it is 1 when the name has not been found, but 0 when it has! 可能应将其称为notfound因为未找到名称时为1 ,但找到时为0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM