简体   繁体   中英

strtok_r Returns Error in tokenizing String

According to the strtok_r man pages:

RETURN VALUE
The strtok() and strtok_r() functions return a pointer
to the next token, or NULL if there are no more tokens.

I tried to compile this function I wrote to tokenize strings with tabs, spaces and \n as delimiters

#include "program.h"
#include <string.h>

/**
 * tokenize_line - Tokenize command input plus any other arguments
 * @s: line to parse
 * @tokens: buffer for filling
 *
 * Return: 1 if success, 0 for failure
 */
int tokenize_line(char *s, char *tokens[])
{
        int i, status;
        char *token, *hold;

        token = strtok_r(s, " \t\n", &hold);
        status = check_if_comment(&token);
        if (status == 1)
                return (0);

        for (i = 0; token && i < 2; i++)
        {
                tokens[i] = token;

                token = strtok_r(NULL, " \t\n", &hold);
        }

        return (1);
}

compiling it with gcc -Wall -Werror -Wextra -pedantic -std=c89 filename_with_this_function.c gives me this error;

In function ‘tokenize_line’:                 
  error: implicit declaration of function
 ‘strtok_r’; did you mean ‘strtok’? [-Werror=implicit-function-de
claration]                                                       
   15 |  token = strtok_r(s, " \t\n", &hold);                    
      |          ^~~~~~~~                                        
      |          strtok                 
rror: assignment to ‘char *’ from ‘int’
 makes pointer from integer without a cast [-Werror=int-conversio
n]                                                               
   15 |  token = strtok_r(s, " \t\n", &hold);                    
      |        ^                        
error: assignment to ‘char *’ from ‘int’
 makes pointer from integer without a cast [-Werror=int-conversio
n]                                                               
   24 |   token = strtok_r(NULL, " \t\n", &hold);                
      |         ^                             

what could be the problem?

The strtok_r function is not part of the C89 standard.

You need to remove the -pedantic -std=c89 options from the command line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM