Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 604
cxzp Avatar asked Dec 26 '22 02:12

cxzp


1 Answers

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

like image 67
Mike Avatar answered Apr 27 '23 20:04

Mike