implicit declaration of function ‘strtok_r’ [-Wimplicit-function-declaration] inspite including <string.h>

穿精又带淫゛_ 提交于 2019-12-01 17:59:40

strtok_r is not a standard C function. You have asked for only C99 by using the -std=c99compiler 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 **);

the problem is that the function strtok_r returns a pointer to char, witch you are trying to assign to an array of char, so in order to make it work you have to declare your line and integer variables as a pointer of char, then allocate the memory with malloc.

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[] = " ";
  char *line;
  char *integer;
  int j;
  line = (char*)malloc(200);
  integer = (char*)malloc(200);
  for(j = 1, str1 = file_contents; ; j++, str1 = NULL) {
    line = strtok_r(str1, delimiter1, &saveptr1);
    printf("%s\n", line);
    if (line == NULL) {
      break;
    }
    printf("end of first\n");

    for (str2 = line; ; str2 = NULL) {
    printf("begin of second\n");
      printf("asdf%s\n", line);
      integer = strtok_r(str2, delimiter2, &saveptr2);
      if (integer == NULL) {
        break;
      }
      printf("%s\n", integer);
    }
  }
}
CS Pei

I am using 4.8.2 and ubuntu 14.04 64bit too. But I got different errors.

    incompatible types when assigning to type 'char[200]' from type 'char *' 
        line = strtok_r(str1, delimiter1, &saveptr1);
             ^

here, line is declared 'char[200]' but strtok_r returns char*.

Similar errors at line for integer = ....

To get rid of these compile errors, you can declare something like char *line;.

For the warning about implicit declaration, check this

Can I use strtok() in a Linux Kernel Module?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!