2

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 ?

James
  • 24,676
  • 13
  • 84
  • 130
oz123
  • 27,559
  • 27
  • 125
  • 187
  • Your initialization looks weird. Do you mean to have just a 0 byte as a start? Then you should initialize with `""` which is a 1-character string with just one `0` byte in it. A way to achieve that much easier would be to just assign `myContacts[i].cFirstName[0] = '\0';` – Jens Gustedt Dec 17 '11 at 22:59

4 Answers4

4

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!

SlavaNov
  • 2,447
  • 1
  • 18
  • 26
  • Except that the loop starts from `i=1`, so the condition must be `i <= num_of_contacts`... Morale: always start with `i=0`... – Yakov Galka Dec 17 '11 at 23:57
  • yes, thanks for pointing it out. Being to used to for loops in Python :-/. – oz123 Dec 19 '11 at 13:30
1

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.

fge
  • 119,121
  • 33
  • 254
  • 329
1

You never end the loop if search does not find any results

Sid Malani
  • 2,078
  • 1
  • 13
  • 13
1

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. 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.

As it happens, I don't understand why your for loop starts at 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. It should perhaps be called notfound since it is 1 when the name has not been found, but 0 when it has!

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490