I am trying to compile this code. It is giving me this error:
format specifies type 'char *' but the argument has type 'char **' [-Werror,-Wformat]
on all my %s
's. What could it be? This is an example from a book. Thanks for any help.
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int response;
char *lName[20] = {0};
char *fName[20] = {0};
char *number[20] = {0};
FILE *pWrite;
FILE *pRead;
printf("\n\tPhone Book\n");
printf("\n1\tAdd phone book entry\n");
printf("2\tPrint phone book\n\n");
printf("Select an option: ");
scanf("%d", &response);
if ( response == 1 ) {
scanf("%s", fName);
printf("\nEnter last name: ");
scanf("%s", lName);
printf("\nEnter phone number: ");
scanf("%s", number);
pWrite = fopen("phone_book.dat", "a");
if ( pWrite != NULL ) {
fprintf(pWrite, "%s %s %s\n", fName, lName, number);
fclose(pWrite);
} else
goto ErrorHandler;
}
else if ( response == 2 ) {
pRead = fopen("phone_book.dat", "r");
if ( pRead != NULL ) {
printf("\nPhone Book Entries\n");
while ( !feof(pRead) ) {
fscanf(pRead, "%s %s %s", fName, lName, number);
if ( !feof(pRead) )
printf("\n%s %s\t%s", fName, lName, number);
}
printf("\n");
}
else
goto ErrorHandler;
}
else {
printf("\nInvalid selection\n");
}
exit(EXIT_SUCCESS);
ErrorHandler:
perror("The following error occurred");
exit(EXIT_FAILURE);
} //end main