Integer out of ARGV Pointer

05/20/2021 11:10 nicileie#1
Hello Community,

i am currently working (more testing) coding on C.
Actually i run in the following issue:

I got an storage error, when trying to save an integer, converted out of an Pointer. I googled this and the other dudes do the same and.. it works lol.

Would be glad if you could take a look. No Compile Errors.

Code:
int main(int argc, char **argv)
{
	utilInit(argv[0]);
	int port = 8111;
	if(argc > 1)
	{
		if(strcmp(argv[1], "-p") == 0)
		{
			if(isdigit(argv[2])){
				int port = ntohs(argv[2]);
			}
		}
	}
	infoPrint("Chat Server running on Port: %i", port);
	const int result = connectionHandler((in_port_t)port);

	return result != -1 ? EXIT_SUCCESS : EXIT_FAILURE;
}
When i exclude "int port = ntohs(argv[2]);" itīs working fine.

Thanks.
05/20/2021 14:06 Jeoni#2
The problem is that you have two variables named "port" instead of one. One is declared at the top and initialized with 8111 and the other is declared where ntohs is used. Since the scope of the latter is only in that "if (isdigit..." block, the print function refers to the first one.
Solution: don't declare a new variable. Just leave out the type ("int") when you assign the result of "ntohs".
05/20/2021 15:05 nicileie#3
Yeah, already got that. Thanks.
My Main issue was: "isdigit" doesnt check the whole String Pointer, itīs just 1 Character check. Works now.