Ok, Let me start off and say Im new to C. Im writting a subnet calculator, in my main function I am asking the user for a subnet mask and storing it in a char variable. Im then passing the variable to another function called "truncat", where I strip the "." out of the address and store each section into an array.
My problem here, is that whenever I assign a section to part of the array and then print that part of the array I get a different number then what it should be.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char str[16];
char ipray[4];
printf("Enter a Netmask :");
scanf("%s", str);
truncat(str, ipray);
return 0;
}
void truncat(char str[], char ipray[])
{
char *tok;
int i = 0;
tok = strtok(str,".");
while (tok != NULL)
{
ipray[i] = *tok;
printf("%d\n\n\n\n", ipray[i]);
tok = strtok(NULL, ".");
i++;
}
}
Here is an example of what happens.
nkowalski@nkowalski-desktop:~/Desktop$ gcc -Wall sub.c -o test2
nkowalski@nkowalski-desktop:~/Desktop$ ./test2
Enter a Netmask :255.255.255.0
50
50
50
48
and no matter what numbers you enter for a subnet mask, it will always output 50,50,50,48