Datatype to be used for IP address

0 votes
asked Jul 18 by Jemimah (120 points)
I am trying to write a code for scanning an IP address from a file. Can anyone please suggest to me on what data type can be used and how we can access an IP from a txt file.

1 Answer

0 votes
answered Jul 20 by Peter Minarik (4,550 points)
edited Jul 22 by Peter Minarik

In what language? C?

After a bit of research, it looks like often IPv4 is often just stored as a char*. (For instance, have a look at this.)

If I would want to store the IP address not as a C-string, I'd create a similar structure in C:

typedef struct
{
    unsigned char f0;
    unsigned char f1;
    unsigned char f2;
    unsigned char f3;
} IPv4;

where f0, f1, f2, f3 represent the 4 fields of the IPv4 address separated by dots.

Alternatively, you can just do a

unsigned char[4];

array as well. An unsigned char can store values from 0 to 255, exactly the range of the IPv4 address fields.

It all depends on what suits you best.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...