deleting all entries of a linked list in c, error

57 Views Asked by At

So, im trying to delete all entries of the linked list in my phonebook lab, and it's reading an error that I don't know how to fix. The error is reading "incompatible types when assigning to type char[50] from type 'int.'

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
    
typedef struct phonebook{ 
    char first_name[70];
    char last_name[50];
    char phone_num[50];
    }entry; //TypeDef to modify structure name


    void addFriend(entry *, int *); //function prototype to add a friend 
    void dltFriend(entry *, int *, int);
    void showBook(entry *, int *);
    void alphaList(entry*, int*);
    void findNum(entry*, int*);
    void randSelect(entry*, int*);
    void dltAll(entry*,int*);

void dltAll(entry*phonebook, int*count){
int i;
for(i=0; i< *count; i++)
{

    do{
        phonebook[i].first_name = '\0';
        phonebook[i].last_name = '\0';
        phonebook[i].phone_num = '\0';
        break;
    }
    while (i <= *count);

}
printf("\nAll contacts deleted\n");
system("pause");
}
2

There are 2 best solutions below

1
Support Ukraine On

The error is reading "incompatible types when assigning to type char[50] from type 'int.'

The messages is due to this line:

phonebook[i].last_name = '\0';
\____________________/   \__/
 This is a char[50]       This is an integer

It makes no sense to assign an integer value to an array. If you want to turn last_name into an empty string do:

phonebook[i].last_name[0] = '\0';

Other notes:

A construct like:

do{
    ...
    break;
}
while (i <= *count);

makes no sense as the break will end the loop after executing ... once. So simply remove the loop.

Also I would expect the function to set *count to zero.

1
Bhanu Satish On
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

typedef struct phonebook{
    char first_name[70];
    char last_name[50];
    char phone_num[50];
    }entry; //TypeDef to modify structure name


    void addFriend(entry *, int *); //function prototype to add a friend
    void dltFriend(entry *, int *, int);
    void showBook(entry *, int *);
    void alphaList(entry*, int*);
    void findNum(entry*, int*);
    void randSelect(entry*, int*);
    void dltAll(entry*,int*);

void dltAll(entry*phonebook, int*count){
int i;
for(i=0; i< *count; i++)
{
        strcpy(phonebook[i].first_name,"NULL");
        strcpy(phonebook[i].last_name,"NULL");
        strcpy(phonebook[i].phone_num,"NULL");
}
printf("\nAll contacts deleted\n");
}
/*int main()
{
    void dltAll();
return 0;
}*/