0

I'm starting with sockets on Windows in C, and I'm trying to understand the use of pointers in C and get a clear idea of why some variables are pointers.

In this code:

int sock;
struct sockaddr_in ServAddr;
unsigned short ServPort;
char *ServIP;
WSADATA wsaData;

ServIP = "192.168.1.6";
ServPort = 50005;

if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) {
    exit(1);
}

sock = socket(AF_INET, SOCK_STREAM, 0);

memset(&ServAddr, 0, sizeof(ServAddr));
ServAddr.sin_family = AF_INET;
ServAddr.sin_addr.s_addr = inet_addr(ServIP);
ServAddr.sin_port = htons(ServPort);

1. Why is ServIP a pointer and ServPort is not?

unsigned short ServPort;
char *ServIP;`

ServIP = "192.168.1.6";

ServPort = 50005;

2. Are these variables pointers?

struct sockaddr_in ServAddr;

WSADATA wsaData;
2
  • ServAddr is a struct, but memset needs a pointer to the struct to clear it. ServIP is declared a pointer, but should be a string (an array of char) char ServIP[32] = "192.168.1.6"; will initialize it. ServPort is a plain unsigned short. Commented Jan 23, 2024 at 20:04
  • 1
    In C, it is normal to pass strings to functions not by value, but rather by reference, by passing a pointer to the first element of the string. That way, it is not necessary to always copy the entire string when passing it to a fucntion. The port number is passed by value (i.e. a simple copy of the value is passed), because it is a simple integer value. "192.168.1.6" is a string literal, which decays to a pointer to the first element of the string literal. Commented Jan 23, 2024 at 20:06

1 Answer 1

3

1. Why is ServIP a pointer and ServPort is not?

ServIP is a char * because that's the type that substantially all C functions require for handling strings. The value is actually a pointer to the first char of the string, which continues up to and including the first char with value 0.

ServPort is not a pointer because there is no reason for it to be one. unsigned short is the type required for this variable's purpose.

The question makes me wonder whether you are among those who take pointer-ness to be analogous to a type qualifier, such as const, that produces a different variant of a base type. This is not the case at all. Pointer types are completely separate types from their pointed-to types. They generally have different size and representation, and are in no way interchangeable with their pointed-to type.* Thus, you use pointers for things that require pointers and not for things that require non-pointers.

2. Are these variables pointers?

struct sockaddr_in ServAddr;

struct sockaddr_in is definitely a structure type, not a pointer type. We know this from the presence of the struct keyword and the absence of * from the type name, together. ServAddr therefore is not a pointer.

WSADATA wsaData;

It is not immediately apparent from the name WSADATA whether that is a pointer type. It is a macro or typedef name that obscures the details (in fact the latter). But if we look it up, we can find its documentation pretty easily, and that reveals that WSADATA is a structure type, not a pointer type. So wsaData is not a pointer.


* With some exceptions related to type void *, which I'm glossing over for the sake of simplicity.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.