-1

I am creating a menu that needs to take in an three inputs from the users.

    char *fullname;
    char *date;
    float sal;
    printf("\nEnter full name: ");

line92

scanf("%s", &fullname);
printf("\nEnter hire date: ");

Line 94

scanf("%s", &date);
printf("\nEnter salary: ");

Line 96

scanf("%d", &sal);

These are the errors I am recieving

Employee.c:92: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
Employee.c:94: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
Employee.c:96: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘float *’

Can I get an explanation of what is causing these issues?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
jenglee
  • 343
  • 2
  • 5
  • 10

2 Answers2

1

There are several problems:

First:

When you use scanf for strings you do not use the &. So just scanf("%s", fullname);.

Second:

Your pointers aren't initialized. Try this instead:

char fullname[256];
char date[256];

This will work as long as you input at most 255 characters.

Third:

Your typing for the last scanf doesn't match. You're passing in a float when you've specified an int in the format string. Try this:

scanf("%f", &sal);
Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • This seems to get rid of the warnings but when you enter the code it sefaults on me – jenglee Oct 12 '11 at 02:22
  • Can you trace which line it segfaults on? – Mysticial Oct 12 '11 at 02:28
  • Ok I figured that it segfaulted because I was doing something wrong. I want to be able to enter a full name "John Doe" but it segfault because of the space. Is there a way to input a string like this – jenglee Oct 12 '11 at 02:41
  • I'm not aware of a C function that will read the whole line. See this question for that: (http://stackoverflow.com/questions/2600528/c-readline-function) – Mysticial Oct 12 '11 at 02:49
  • Thank you for your help, I have gone a different way – jenglee Oct 12 '11 at 02:58
0

The warnings are pretty self-explanatory. When you call scanf with a %s format specifier you need to provide it a pointer to the first element of a char array where the string can be copied to. You're not doing that, instead you're giving it the address of a pointer to a char.

char[100] fullname;
scanf( "%s", fullname );

The same problem exists for date. Also, be aware that using the code above, a buffer overflow will occur if the user enters a string equal to or more than 100 characters in length.

If you're using MSVC you can use the scanf_s function instead that requires you to enter the length of the buffer. However, this function is Microsoft specific, hence, non-portable.

scanf_s( "%s", fullname, 100 );

For salary, the problem is that the format specifier is %d, which is used for reading ints, not floats. Use

scanf( "%f", &sal );
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Praetorian
  • 106,671
  • 19
  • 240
  • 328