Hello,
I have the following function, which is based on an earlier example (which works) where the code was executed in main.
So now I'm trying to execute the same logic, but with the code in a function.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void extract_subnet(char ip_address[16],char *ip_subnet[12]);
int main()
{
char source_ip_address[16];
char source_ip_subnet[12];
strcpy(source_ip_address,"192.168.200.150");
strcpy(source_ip_subnet,"");
extract_subnet(source_ip_address,&source_ip_subnet);
printf("Source IP Address: %s\n",source_ip_address);
printf("Source IP Subnet: %s\n",source_ip_subnet);
return 0;
}
void extract_subnet(char ip_address[16],char *ip_subnet[12])
{
printf("Input IP address: %s\n",ip_address);
int i;
int period_count = 0;
char ch;
int length = strlen(ip_address);
for(i=0;i<length;i++)
{
ch = (ip_address[i]);
if (ch == '.')
{
period_count = period_count + 1;
}
if (period_count == 2)
{
break;
}
ip_subnet[i] = ip_address[i];
}
ip_subnet[i] = '\0';
return;
} // extract_subnet
The issue that I'm observing, is that in the extract_subnet function; when the counter i = 3 and ch should equal the first period ('.') in the IP address (i.e. 192. )
I can see instead via the debugger that the value in ch is 0
I don't understand why this is happening.
I have confirmed via this statement
Code:
printf("Input IP address: %s\n",ip_address);
That the ip_address is being passed correctly to the function.
Thanks in advance
VW