1

I'm getting the below warning on a C program. It does run. However, how do I fix the warning?

warning: format specifies type 'char *' but the argument has type 'char (*)[100]' [-Wformat]
            printf("%s\n", &buf);

The code looks like this:

FILE *fp = fopen(filename, "r");
int len = 0;
char buf[100];

printf("\n sorting \n");
while (fscanf(fp, "%s", buf) != EOF)
{
    printf("%s\n", &buf);
    len++;
}

printf("Sorted\n");
Acorn
  • 24,970
  • 5
  • 40
  • 69
Nick_Nick
  • 51
  • 6

1 Answers1

5

This line is wrong:

    printf("%s\n", &buf);

As the warning says, you need to match %s with a char * parameter. buf is an array, so just using its name in this context will cause it to decay into a pointer to its first element - exactly what you want:

    printf("%s\n", buf);

Using the & explicitly passes a pointer to your array; it'll be the same literal address, but has a different type, and that's what your warning is saying.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469