0

I'm quite new to c++, more experienced with Java, so I don't fully understand c++ variable declaration and pointers.

That being said I'm trying to connect a socket to a server, for which I need to use getaddrinfo() to get the address and I have defined(there are numbers in place of x's):

const char* ipAddress = "xx.xx.xx.xxx";
const char* port = "xxxx";
const struct addrinfo *hints, *res;

(I only added the consts because I thought it might fix my problem)

However when I try to call

int result = getaddrinfo(ipAddress,port,NULL,res);

I get the error "No matching function for call to getaddrinfo()".

What am I doing wrong here?

Chuckster
  • 69
  • 1
  • 4
  • As far as I can see from documentation, `getaddrinfo` accepts `addrinfo**` as a last argument, while you are passing `addrinfo*`. Is it a typo (i.e. `getaddrinfo(ipAddress,port,NULL,&res)`)? – Algirdas Preidžius Feb 25 '19 at 15:42
  • `getaddrinfo` takes a `addrinfo**` as its final argument, which is a pointer to a pointer. You're passing a `const struct addrinfo*`. which is a const pointer. Remove the const, and then pass the address of `res` (`&res`) to get the pointer `res`, which will fix your problem. – xEric_xD Feb 25 '19 at 15:46
  • That worked, thanks! But I still don't understand what the * and & operators do here, and why my hints parameter is able to pass without being constant while the documentation specifies a const struct addrinfo*? – Chuckster Feb 25 '19 at 15:46
  • 1
    If you do not fully grasp what pointers are yet, and how to use them, I recommend [reading a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to help get you started. – xEric_xD Feb 25 '19 at 15:50
  • The expression `&res` [takes the address](https://en.cppreference.com/w/cpp/language/operator_member_access) of the pointer `res`, yielding a pointer to pointer (`type addrinfo**`, as the function expects). This allows the function to set your local `res` variable to point at the result, in memory owned by the library. You might benefit from a good book if you can't read the basic syntax on your own yet. – Useless Feb 25 '19 at 15:51
  • I will look into it, thank you!! – Chuckster Feb 25 '19 at 15:52
  • Try to read and understand the *entire* error message. – n. m. could be an AI Feb 25 '19 at 16:17

0 Answers0