c - strcpy with destination pointer and return value -


My plain bit is a bit messy, and I currently try to understand why the first works and not the second .

  Four * Returns some (zero) {four * S = (four *) Molk (5 + 1); Strapepi (S, "Hello"); Return S; } Zero dontReturnSomething (char * dest) {char * s = (four *) malloc (5 + 1); Strapepi (S, "Hello"); Dest = malloc (strlen (s) + 1); Strapepy (Dest, S); Free (s); } Int main (int argc, char * argv []) {char * first = return some (); Char * second = NULL; DontReturnSomething (second); Printf ("first:% s | second:% s", first, second); }   

Is not it basically the same thing?

You need an indicator to return an indicator through an indicator. Declare as dest as char ** dest .

  void dontReturnSomething (char ** dest) {char * str = "Hello"; * Dest = malloc (strlen (str) + 1); Strcpy (* dest, str); }   

Call the function like this:

  dontReturnSomething (& amp; seconds); // second     to the address

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -