cxzp
cxzp

Reputation: 672

Not sure why the compiler is complaining ... implicit declaration of function strchrnul

This is my code:

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

static int valid_line(char *cur_line) {
    if (*cur_line == '#') {
        return 0;
    }

    char *end = strchrnul(cur_line, '#');

    while(end > cur_line && isspace(*end)) end--;
    return (!(*cur_line == *end));
}

I am going through the line and am getting rid of leading and trailing white spaces and anything that occurs after the '#' (including the '#').

My compiler is saying this:

parser.c:20:2: warning: implicit declaration of function ‘strchrnul’ [-Wimplicit-    function-declaration]
parser.c:20:14: warning: initialisation makes pointer from integer without a cast [enabled by default]

EVen though I have string.h above.

Could someone please explain.

Upvotes: 5

Views: 3436

Answers (2)

Mike
Mike

Reputation: 49393

strchrnul() is a GNU extension, and you can get this function included warning free via a feature test macro.

#define _GNU_SOURCE  // required for strchrnul()

#include <stdio.h>
#include <ctype.h>
#include <string.h>  // required for strchrnul()
#include <stdlib.h>

static int valid_line(char *cur_line) {
    if (*cur_line == '#') {
        return 0;
    }

    char *end = strchrnul(cur_line, '#');

    while(end > cur_line && isspace(*end)) end--;
    return (!(*cur_line == *end));
}

Please note from the second linked man page, the placement of the #define is important:

NOTE: In order to be effective, a feature test macro must be defined before including any header files

Upvotes: 7

ouah
ouah

Reputation: 145829

If you are using gcc compiler, don't use -std=c89 or -std=c99 but rather use -std=gnu89 or -std=gnu99 as strchrnul is a GNU extension.

Upvotes: 4

Related Questions