Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8861517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:30:34+00:00 2026-06-14T15:30:34+00:00

I want to parse an int from a string ( char* ) in C.

  • 0

I want to parse an int from a string (char*) in C. I’m writing a linux kernel module for embedded linux. I’m trying to use simple_strtol found here.

My tokens I’m parsing in an ideal situation will be numbers. The problem I have is "0" is a valid input. strtol returns 0 if a value could not be parsed (error). So that means that the following code:

char* token = "should_fail";

char **endptr;
char value = simple_strtol(token, endptr, 10); // 10 is the base to convert to
if (!**endptr){
    printf("failed!");
} else {
    printf(value);
}

char* token = "0"; // should pass
char **endptr;
char value = simple_strtol(token, endptr, 10); // 10 is the base to convert to
if (!**endptr){
    printf("failed!");
} else {
    printf(value);
}

prints out 0 in both cases, when it should fail in the first case. I am trying to use that *endptr argument to check to see if the conversion is successful, but its not working. According to what I’ve researched, *endptr is “A pointer to the end of the parsed string will be placed here”. And I believe if a conversion failed the pointer will point to nothing so I can identify that failure.

Does anybody know how I can parse "0" properly and get a return value of 0 while still identifying a failed parse, and not having it return 0?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:30:35+00:00Added an answer on June 14, 2026 at 3:30 pm

    You should not create a pointer-to-pointer variable for endptr. Instead declare a single pointer, and pass the address of that with the unary & operator:

    char *endptr;
    
    simple_strtol(token, &endptr, 10);
    
    if (endptr == NULL) ...
    

    I didn’t read the manual page but basically copied what the OP had used. Froom the manual page:

    If the subject sequence is empty or does not have the expected form, no conversion is performed; the value of str is stored in the object pointed to by endptr, provided that endptr is not a null pointer.

    This means that if strtol (or your alternative if it’s working as specified) returns 0 and endptr == token then the string was invalid.

    So the check should be

    if (value == 0 && endptr == token)
    {
        /* String is not a valid number */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get a JSON String from a Servlet und want to parse this string
I want to parse a xml file using xquery in Ruby, I found this
i just want to parse two values from a html file . there will
I'm trying to parse a string into smaller ones, extracting some values and then
I'm trying to connect to Linux using SharpSSH, but am unable to. I want
%{ #include <stdio.h> #include <string.h> void yyerror(const char *str) { fprintf(stderr,error: %s\n,str); } int
I want to parse the values from the JSON-Post into Java-Variables. But they are
Hello guys im having little problem here, Firstly i want to parse string which
I'm trying to write a program that reads text from external file (string string
I've made a little forum and I want parse the date on newest posts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.