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

  • SEARCH
  • Home
  • 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 9092257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:47:51+00:00 2026-06-16T22:47:51+00:00

I am having problem with inet_aton to convert a network address. The code below

  • 0

I am having problem with inet_aton to convert a network address. The code below works fine to convert the address 10.0.0.1

char *x1;
struct sockaddr_in si_other;
inet_aton("10.0.0.1", &si_other.sin_addr);
printf("si_other.sin_addr =%lu\n",si_other.sin_addr);
x1 = inet_ntoa(si_other.sin_addr);
printf("x1=%s\n",x1);

It outputs:

si_other.sin_addr =16777226
x1=10.0.0.01

No problem so far. However, the function works weird when 010.000.000.001 is passed

char *x2;
struct sockaddr_in si_other2;
inet_aton("010.000.000.001", &si_other2.sin_addr);
printf("si_other2.sin_addr =%lu\n",si_other2.sin_addr);
x2 = inet_ntoa(si_other2.sin_addr);
printf("x2=%s\n",x2);

outputs:

si_other.sin_addr2 =16777224
x2=8.0.0.01

The function works fine when 192.168.0.1 and 192.168.000.001 are passed.

Can anyone explain me what is the problem and how I can fix the problem? (note: I need to pass the IP address as 010.000.000.001 in my code)

  • 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-16T22:47:53+00:00Added an answer on June 16, 2026 at 10:47 pm

    The leading 0 is being interpreted as indicating the number is octal. 010 (oct) == 8 (dec). You need to modify the input to inet_aton to avoid this, or convert it yourself in a different way.

    const char *str = "010.000.000.001";
    inet_aton(str[0] == '0' ? str+1:str, &si_other.sin_addr);
    

    Is the simplest solution, but it would be better to fix whatever (snprintf?) produces the string in the first place to avoid the confusion.

    (As it stands that solution won’t work with a number of edge cases still, including “001.0.0.1”, “0xF.0.0.1”, “1” and many more valid IPv4 addressees).

    You can trivially “normalise” your input using sscanf, even if you can’t control how it gets generated in the first place (although that really ought to be a bug whever it came from in my view):

    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
      const char *str="010.020.030.040";
      int parts[4];
      sscanf(str, "%d.%d.%d.%d", parts+0, parts+1, parts+2, parts+3);
      char *out = NULL;
      asprintf(&out, "%d.%d.%d.%d", parts[0], parts[1], parts[2], parts[3]);
      printf("%s\n", out);
      free(out);
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having problem with datagrid view. I have attached an image with the code
I am having problem with drawing multiple lines during repaint. The code is as
I'am having problem in accessing json array elements. Below is the response when i
Having problem with this bit of code qith jQuery. it should pick the values
I am having a problem getting the example code shown in the python docs
Im having problem's adding map markers to gmaps using jquery. Here is my code
am having problem in Saving some data in my Java code. I have like
having problem with this line of code, compiler error on getInt method. because the
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
Im having problem in my UIscrollView ,this is what I have done: Whenever a

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.