Confusion with Pointers and References

I'm working on some WinSock code and I'm having some trouble with some of the syntax. First off we have the WSADATA structure:

1
2
3
4
5
6
7
8
9
typedef struct WSAData {
  WORD           wVersion;
  WORD           wHighVersion;
  char           szDescription[WSADESCRIPTION_LEN+1];
  char           szSystemStatus[WSASYS_STATUS_LEN+1];
  unsigned short iMaxSockets;
  unsigned short iMaxUdpDg;
  char FAR       *lpVendorInfo;
} WSADATA, *LPWSADATA;


I think I get this part. This basically says that WSADATA is an object of type struct WSAData and *LPWSADATA is a pointer to an object of type struct WSAData.

Then we have the WSAStartup declaration:

1
2
3
4
int WSAStartup(
  _In_  WORD      wVersionRequested,
  _Out_ LPWSADATA lpWSAData
);


The second parameter is a pointer to an object of type struct WSAData.

This is where I'm getting confused. When I look at the WSAStartup call in the WinSock code:

1
2
3
4
5
6
7
8
9
WSADATA wsaData;
int iResult;

// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
    printf("WSAStartup failed: %d\n", iResult);
    return 1;
}


The second parameter is a reference, which in the definition, is a pointer to the object. Can someone explain to me how this all fits together?
Last edited on
The second parameter is a reference which in the definition is a pointer to the object.


It's not a reference. '&' in this context is the "address of" operator, and not the reference operator.

&wsaData is giving you the address of (aka the "pointer to") the 'wsaData' object.


'&' is the reference operator when bundled with a typename:

1
2
3
4
5
int& foo = whatever;  // <- foo is a reference, because the '&' is bundled with the typename 'int'

// but when you don't use a typename and are just using an object name, it's address of:
int* ptr = &whatever;  // <- here, the & is address of because it is bundled with an object,
   // and not a type name 
Topic archived. No new replies allowed.