I know this is pretty simple for most of you but I am trying to increment an ip address by +1 in a loop.
Example:
for(double ip = 1.1.1.1; ip < 1.1.1.5; ip++)
{
printf("%f", ip);
}
Basically all I am trying to do is increment the ip by +1 in a for loop. I don't know what type of variable to store the ip in and don't know how to increment it. Whenever I run the program I get an error saying that the number has too many decimal points. I've also seen on the internet that you have to store ip's in a character array, but you cannot increment a character array (that I know of). What variable type should I store the ip in/how should I approach this? Thank you.
If you are searching the same subnet 1.1.1. the entire time you can store you last octet as the only int.
Loop through it increaseing the lastoctet each time and appending it to your string.
I am not to familiar with C syntax so
If you are searching more octets or need t oincrease other digits you can store that octet as a seperate integer and reform your string before or during your looping.
IPV4 addresses are 32bits wide.
Why not take a 32bits wide unsigned integer (
uint32_t
for example) initialise it to any start value, count it up and convert the result to the dotted string version of the ip-address using the appropriate libc functions?For further references on the latter please see the man-pages for the
inet_XtoY()
family of functions.A naive implementation (no
inet_pton
) would user 4 numbers and print them into achar
array*Edit: A new implementation inspired by alk