-1

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");
}
galapagos
  • 35
  • 5
  • 1
    You cannot assign a new value to any array. If you want to assign a nul-terminator to each of your character arrays. you need to use array index: `phonebook[i].first_name[0] = '\0'`. Same for the other arrays. – Gerhardh Oct 22 '21 at 12:33
  • so that works but it just clears the entries rather than deleting them, is there a way I could work around that. thanks! – galapagos Oct 22 '21 at 12:36
  • @galapagos you can't delete these entries. The only thing you can do ist mark them as deleted. If you want actually delete them, you need to look into dynamic memory allocation, but this may be too early for the moment. – Jabberwocky Oct 22 '21 at 12:40
  • 1
    What is the purpose of the `do-while` loop? – Support Ukraine Oct 22 '21 at 12:41
  • a while loop inside a for loop with a break, quite strange – Ôrel Oct 22 '21 at 12:42
  • what is the purpose of passing `count` as a pointer in `dltAll`? it is not modified in this code. – tstanisl Oct 22 '21 at 12:43
  • Actually there is no linked list in your code.... – Jabberwocky Oct 22 '21 at 12:56

2 Answers2

1

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.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0
#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;
}*/
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Tyler2P Oct 22 '21 at 18:52