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

Edited 6 Years Ago by Eternal49: n/a

Your problem is this line right here

ipray[i] = *tok;

Which doesn't do what you think it does.

Take a look at this version which is a simplified version of your code and is correct...Now all you have to do is convert the values to integers or copy them to the character array.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void truncat(char str[], char ipray[]);

int main()
{
	char str[16];
	char ipray[4];

	printf("Enter a Netmask :");
	scanf("%s", &str[0]);

	truncat(str, ipray);

	return 0;
}

void truncat(char str[], char ipray[])
{
	char *tok;
	int i = 0;

	tok = strtok(str,".");

	while (tok != NULL)
	{
		fprintf(stdout, "tok->%s\n", tok);
		tok = strtok(NULL, ".");
	}
}

Edited 6 Years Ago by gerard4143: n/a

instead of printing it, how can I assign it to variables or a char array? So if I enter 255.255.255.0 for a netmask, it will assign

ipray[0] = 255
ipray[1] = 255
ipray[2] = 255
ipray[3] = 0

I could then use atoi() to convert them to int and I already wrote another function that converts decimal to binary. So I should hopefully be good after I get this figured out, unless I run into problems when doing the actual math.

Could you also explain line 5 and line 29?

Thanks,

instead of printing it, how can I assign it to variables or a char array? So if I enter 255.255.255.0 for a netmask, it will assign

ipray[0] = 255
ipray[1] = 255
ipray[2] = 255
ipray[3] = 0

I could then use atoi() to convert them to int and I already wrote another function that converts decimal to binary. So I should hopefully be good after I get this figured out, unless I run into problems when doing the actual math.

Could you also explain line 5 and line 29?

Thanks,

If the values are never bigger than 255 then you could use
unsigned chars for the array type which have a value range 0 - 255.
To convert then I would use atoi.

Just a suggestion, maybe you'd be better off without strtok()/atoi() altogether, using sscanf() instead. A minimum example to try out ..

#include <stdio.h>

int main()
{
  const char * str = "255.255.255.0";
  int arr[4];

  int res = sscanf(str, "%d.%d.%d.%d", &arr[0], &arr[1], &arr[2], &arr[3]);

  /* Did sscanf() convert 4 fields? */
  if(res == 4) {
    printf("OK: %d-%d-%d-%d\n", arr[0], arr[1], arr[2], arr[3]);
  }
  else {
    printf("Failed, res = %d\n", res);
  }

  return 0;
}

Thanks mitrmkar!

Also how come you used the ampersands(&) in front of the 'arr' variable in line 8 but not in line 12?

Thanks,

Edited 6 Years Ago by Eternal49: n/a

Because --
1) when you are outputting data with printf() you pass the value into the function.
2) when you are inputting values with scanf() you pass the address of the variable so the value can be passed back.

This article has been dead for over six months. Start a new discussion instead.