-2

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
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 2
    Please don't tell me the book used that indentation and lack of spacing. Anyway, you're giving an array of `char *` to `fprintf`, but saying you'll pass a (`const`) `char *` with `%s`. And you're really opening yourself up to easy buffer overruns with the input not stopping at the end of the array. And none of the input is checked. Just because `feof` says you aren't at the end doesn't mean the next read will be successful. – chris Jul 21 '14 at 02:03
  • [Explanation of why `while( !feof(pRead) )` is wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – M.M Jul 21 '14 at 02:09

4 Answers4

1

As you can probably guess, something here is wrong:

char *lName[20] = {0};
char *fName[20] = {0};
char *number[20] = {0};

You see, when you declare an array like this:

int a[20];

what you're saying to the compiler is: "Give me an array with 20 ints, and make the variable "a" point to the beginning of them". It's like saying a is actually an int*, not an int. So, if you declare

int* a[20];

What you're declaring is an array of "pointers to integers". It's like a is an int**, also not an int.

Applying that to your problem:

char lName[20] = {0};
char fName[20] = {0};
char number[20] = {0};

You see, now it's declaring 3 arrays of chars, and the variables lName, fName and number will point to the beginning of the arrays and have type char*, as is expected by scanfwhen you use %s.

Sathish
  • 3,740
  • 1
  • 17
  • 28
Not a real meerkat
  • 5,604
  • 1
  • 24
  • 55
0

Your allocating pointers.

This

char *lName[20] = {0};
char *fName[20] = {0};
char *number[20] = {0};

should be this

char lName[20] = {0};
char fName[20] = {0};
char number[20] = {0};
Jahaja
  • 3,222
  • 1
  • 20
  • 11
  • 1
    Or, more simply, `char lName[20] = "";`. Or perhaps they're supposed to be arrays of `char*` pointers, and you need to call `scanf` with an element of the array. – Keith Thompson Jul 21 '14 at 02:07
0

This is probably a typing error.

char *lName[20] = {0};

It should be

char lName[20] = {0};

Similar changes should be made to the definitions of fName and number.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0
char *lName[Val] = {0};
char *fName[Val] = {0};
char *number[Val] = {0};

are equivalent to two dimensional array. But you are using it as one dimensional array. Just do the following

char lName[20] = {0};
char fName[20] = {0};
char number[20] = {0};

And it will solve your problem.

Naseef Chowdhury
  • 2,357
  • 3
  • 28
  • 52