1

I have to create a program which adds records to a simple phone book. The code is below, but it doesn't work - function ends and then it stucks on declaring struct record x and doesn't want to display my added record - the program breaks down. When I put this part of code on the end of the function (but instead of "struct record x = array[0];" I put "struct record x = (*array)[0]") it works - record is printed. So I guess the problem is something about pointers, but I'm struggling and I really couldn't find out what's wrong. I remember that few weeks ago I created a program which was very similar but it was adding a new record to an array of integers, with fixed values and it was working well, so maybe there's something with structures that I don't know about. Thanks for any help!

I know the program isn't done yet and I know that I didn't make any action for temp_array == NULL, it'll be done after I found out what's going on.

struct record {
    char f_name[SIZE];
    char name[SIZE];
    long int phone;
};

int add_record(struct record** array, int n)
{
    struct record* temp_array = malloc((n+1) * sizeof(struct record));
    if (temp_array == NULL)
    {
        free(temp_array);
        return -1;
    }
    int i;
    for (i=0; i < n; i++)
    {
        temp_array[i] = (*array)[i];
    }
    struct record new_record;
    printf("\nAplly data.");
    printf("\nFirst name: "); /*fgets(new_record.f_name, SIZE, stdin);*/ scanf("%s", &new_record.f_name);
    printf("Surname: "); /*fgets(new_record.name, SIZE, stdin);*/ scanf("%s", &new_record.name);
    printf("Phone number: "); scanf("%d", &new_record.phone);
    temp_array[n] = new_record;
    free (*array);
    *array = temp_array;
    //struct record x = (*array)[0];
    //puts(x.f_name); puts(x.name); printf("%d", x.phone);
    return 0;
}

main()
{
    struct record* array; int n = 0;
    int choice;
    printf("\n1. Add record\n2. Delete record\n3. Find record\n0. Exit\n\nChoose action: ");
    scanf("%d", &choice);
    switch(choice) {
        case 0: printf("\nKsiazka zostala zamknieta.\n"); return;
        case 1: add_record(&array, n); n++; break;
        case 2: return;
        case 3: return;
        default: printf("Wrong choice.\n\n"); return;
    }
    struct record x = array[0];
    puts(x.f_name); puts(x.name); printf("%d", x.phone);
}
10
  • What do you mean by "it stucks on declaring struct record x [...] the program breaks down"? Is this a compile error (post the whole error) or runtime problem (be as descriptive as possible)? Dec 24, 2014 at 1:18
  • struct record* array=NULL;, and use %ld for long int
    – BLUEPIXY
    Dec 24, 2014 at 1:27
  • There are several things wrong with this program. If temp_array == NULL then you do not want to do free(temp_array) because temp_array was never allocated a valid address. You call add_record... with &array which has type struct record **array (it's a pointer to a pointer). You should just pass array since it's already a pointer. When you call add_record with array, the struct pointer has no valid address assigned to it, and no data assigned to it if it did. So the function of add_record will fail.
    – lurker
    Dec 24, 2014 at 1:29
  • 2
    @lurker it won't hurt anything, but the lack of need to do it in the first place is certainly true. free() ing NULL is a defined no-op behavior.
    – WhozCraig
    Dec 24, 2014 at 1:32
  • 1
    @BLUEPIXY Thanks, but could You describe me why is that? Isn't free() setting pointer to NULL actually?
    – zuroslav
    Dec 24, 2014 at 10:25

1 Answer 1

0

struct record* array=NULL;, and use %ld for long int – BLUEPIXY

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.