C passing string to function issue

astronautlevel

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.

LoztInSpace

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Passing Java ArryList<String> to JNI C function and print the list in C

From Dev

Passing a string to pthread function

From Dev

Passing a String Array to a Function

From Dev

Trouble with passing string to function

From Dev

why does passing C string into function as char* cause runtime error (but passing C string defined as char[] is ok)?

From Dev

issue in id passing into function

From Dev

passing string to ggplot function

From Dev

C++ passing vector<string> element to function causes SIGSEGV crash

From Dev

passing string to a function

From Dev

passing a string into a function

From Dev

C++: Passing string arrays to function WITHOUT USING VECTORS

From Dev

Passing string into function and modifying in C (trouble with pointers)

From Dev

Difference in different way of defining string in c++ and passing these as parameter in function

From Dev

Passing a pointer (string) to a C function

From Dev

C Programming , Changing String Value in function without passing the size as parameter

From Dev

Passing a string vector to a function and function prototype issue c++

From Dev

Solidity issue passing parameters to a function

From Dev

Passing string variable to function containing system() command in C

From Dev

Passing swift string to c function char *

From Dev

Issue in passing function in matchDispatchToProps -- React

From Dev

Passing string into a lambda function

From Dev

Issue passing function arguments dynamically to another function

From Dev

Passing input string to a function in C

From Dev

Fortran and C interoperability: passing char *string as a function's argument to Fortran

From Dev

Error When Passing String array to function in C++

From Dev

Passing a String as a Function in a Linked List in C

From Dev

Passing c-style string to a template function

From Dev

Issue passing function to child component

From Dev

Passing string in an onclick function

Related Related

  1. 1

    Passing Java ArryList<String> to JNI C function and print the list in C

  2. 2

    Passing a string to pthread function

  3. 3

    Passing a String Array to a Function

  4. 4

    Trouble with passing string to function

  5. 5

    why does passing C string into function as char* cause runtime error (but passing C string defined as char[] is ok)?

  6. 6

    issue in id passing into function

  7. 7

    passing string to ggplot function

  8. 8

    C++ passing vector<string> element to function causes SIGSEGV crash

  9. 9

    passing string to a function

  10. 10

    passing a string into a function

  11. 11

    C++: Passing string arrays to function WITHOUT USING VECTORS

  12. 12

    Passing string into function and modifying in C (trouble with pointers)

  13. 13

    Difference in different way of defining string in c++ and passing these as parameter in function

  14. 14

    Passing a pointer (string) to a C function

  15. 15

    C Programming , Changing String Value in function without passing the size as parameter

  16. 16

    Passing a string vector to a function and function prototype issue c++

  17. 17

    Solidity issue passing parameters to a function

  18. 18

    Passing string variable to function containing system() command in C

  19. 19

    Passing swift string to c function char *

  20. 20

    Issue in passing function in matchDispatchToProps -- React

  21. 21

    Passing string into a lambda function

  22. 22

    Issue passing function arguments dynamically to another function

  23. 23

    Passing input string to a function in C

  24. 24

    Fortran and C interoperability: passing char *string as a function's argument to Fortran

  25. 25

    Error When Passing String array to function in C++

  26. 26

    Passing a String as a Function in a Linked List in C

  27. 27

    Passing c-style string to a template function

  28. 28

    Issue passing function to child component

  29. 29

    Passing string in an onclick function

HotTag

Archive