3
char *searcharray = malloc(size);
for (i = 0; i < size; i++)
{
  fscanf(filePtr, "%c", searcharray[i]);
}

Here is my code. And Everytime i keep getting the warning message:

warning: format '%c' expects argument of type 'char *', but argument 3 has type 'int'

How is the variable searcharray being determined as an int?

Jacomus
  • 33
  • 1
  • 5
  • You're dereferencing the character pointer. Try using pointer notation: `(searcharray + i)` – ciphermagi Mar 17 '14 at 19:14
  • @abelebjt %c expects a `char*` in scanf, it " Matches a sequence of characters whose length is specified by the maximum field width (default 1); the next pointer must be a pointer to char, ..." – Adrian Ratnapala Mar 17 '14 at 19:15

1 Answers1

11

What's happening:

  1. searcharray[i] has type char.
  2. In a varargs function, the char will be promoted to an int.

Your bug:

  1. fscanf expects the variables that it will place data into to be passed by pointer.
  2. So you should be doing:

    fscanf(filePtr, "%c", &searcharray[i]);
    
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • Wow. I never new about promotion of variadic args. Where is it specified? – Adrian Ratnapala Mar 17 '14 at 19:16
  • @AdrianRatnapala: See __C 2011, Section 6.5.2.2 Functions Calls, Paragraph 6__. Notably, floats are also promoted to doubles. Which is why printf("%lf", ...); and `printf("%f", ...)` both handle doubles. – Bill Lynch Mar 17 '14 at 19:19
  • @AdrianRatnapala: Or perhaps this will be helpful: http://stackoverflow.com/questions/1255775/default-argument-promotions-in-c-function-calls – Bill Lynch Mar 17 '14 at 19:27
  • Thank you so much for your help. I was sitting and staring at this the longest trying to figure out what was wrong. – Jacomus Mar 18 '14 at 05:12