Home > Net >  Format '%[^_' expects argument of type 'char *' but argument 3 has type 'ch
Format '%[^_' expects argument of type 'char *' but argument 3 has type 'ch

Time:12-29

I am receiving a warning in the for statement:

for (; p < p_end && sscanf(p, "%[^_]%n", &buf, &n); p  = (n 1))

Code

#include <stdio.h>
#include <string.h>

void find_integers(const char* p) {
    size_t s = strlen(p) 1;
    char buf[s];
    const char * p_end = p s;
    int n;
    /* tokenize string */
    for (; p < p_end && sscanf(p, "%[^_]%n", &buf, &n); p  = (n 1))
    {
        int x;
        /* try to parse an integer */
        if (sscanf(buf, "%d", &x)) {
            printf("got int :) %d\n", x);
        }
        else {
            printf("got str :( %s\n", buf);
        }
    }
}

int main() {
    const char *  line = "Foo_bar_Baz_23_25_27";
    find_integers(line);
}

Trying to use above code am getting this warning:

Format '%[^_' expects argument of type 'char *' but argument 3 has type 'char(*)[(sizetype)(s)]'

How would I get rid of this warning? Thank You.

CodePudding user response:

As the error message states, the %[ format specifier expects a char * as an argument, but the argument you're passing, i.e. &buf, has type char (*)[s], i.e. a pointer to a variable length array.

An array decays into a pointer to its first element in most expressions, so get rid of the & on the argument.

for (; p < p_end && sscanf(p, "%[^_]%n", buf, &n); p  = (n 1))
  •  Tags:  
  • c
  • Related