I have the following code to tokenize a string containing lines separated by \n
and each line has integers separated by a \t
:
void string_to_int_array(char file_contents[BUFFER_SIZE << 5], int array[200][51]) {
char *saveptr1, *saveptr2;
char *str1, *str2;
char delimiter1[2] = "\n";
char delimiter2[2] = " ";
char line[200];
char integer[200];
int j;
for(j = 1, str1 = file_contents; ; j++, str1 = NULL) {
line = strtok_r(str1, delimiter1, &saveptr1);
if (line == NULL) {
break;
}
for (str2 = line; ; str2 = NULL) {
integer = strtok_r(str2, delimiter2, &saveptr2);
if (integer == NULL) {
break;
}
}
}
}
(Have included only the relevant function here, the complete, if required, is here.)
However, when I try to compile this code using:
gcc -m64 -std=c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes file_read.c
I get the following warnings:
file_read.c:49:5: warning: implicit declaration of function ‘strtok_r’ [-Wimplicit-function-declaration]
line = strtok_r(str1, delimiter1, &saveptr1);
^
file_read.c:49:10: error: incompatible types when assigning to type ‘char[200]’ from type ‘int’
line = strtok_r(str1, delimiter1, &saveptr1);
^
file_read.c:59:15: error: incompatible types when assigning to type ‘char[200]’ from type ‘int’
integer = strtok_r(str2, delimiter2, &saveptr2);
^
Line nos 49 and 59 correspond to the strtok_r
call.
As you can see, I have included string.h
in my file (which is where strtok_r
is declared), still I get the implicit declaration warning for strtok_r
.
Any insights as to how I can remove the warning is appreciated.
I am using gcc 4.8.2 on ubuntu 14.04 64-bit desktop.
strtok_r
is not a standard C function. You have asked for only C99 by using the -std=c99
compiler flag, so the header files (of glibc) will only make the standard C99 functions in string.h
available to you.
Enable extensions by using -std=gnu99
, or by defining one of the extensions, shown in the manpage of strtok , that supports strtok_r before including string.h
. E.g.
#define _GNU_SOURCE
#include <string.h>
Note that the code have other problems too, strtok_r
returns a char *
, but you are trying to assign that to a char array in integer = strtok_r(str2, delimiter2, &saveptr2);
. Your integer
variable should be a char *
Same problem with GCC 7.4.2 on Debian
Solved using __strtok_r
or -std=gnu99
or adding a protoype after includes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
extern char *strtok_r(char *, const char *, char **);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With