I'm having an issue passing a char* to a function in C.
I have a method dns_magic
, where one of the parameters is a char *addr
. In addition, I have a char temp[20]
, which contains a string (typically something like irc.rizon.net
). Right before I call dns_magic(temp)
, I do a printf("Temp: %s", temp)
and it prints as expected. In the first line of dns_magic
, I call printf("Addr: %s", addr)
, and it prints nothing (as if the string is null).
Here's the relevant part of the code:
printf("Temp: %s", temp);
res = dns_magic(temp, conf->port, &hints);
and
struct addrinfo *dns_magic(char *addr, int port_i, struct addrinfo hints)
{
printf("Addr: %s", addr);
Could someone help tell me what I'm doing wrong? I've tried changing dns_magic to take a char[20] and disabling compiler optimization, but neither seems to have fixed the issue.
Any help is appreciated.
Turn on compiler warnings and get rid of them.
I'd suggest that your 3rd parameter is messing things up. You declare a whole struct but pass in a pointer to one. The stack is messed up and the function can't properly find the parameters passed.
You should make the last parameter a struct addrinfo *hints
with appropriate const
modifiers.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments