pass strings by reference in C -


I'm having trouble finding out how to pass the string back through the parameters of the function. I'm new to programming So, I think that this is probably the starting question. Any help you can give will be most appreciated. This code is a bug fault, and I am not sure why, but I am providing my code to show what I have done so far.

I've made it a community wiki, so edit.

PS This is not homework.

This is the original version

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; #include & lt; String.h & gt; Zero fn (char * baz, char * foo, char * times) {char * pch; / * This is the part that I'm having trouble with * / pch = strtok (baz, ":"); Fu = Molok (Stellon (pH)); Strcpy (foo, pch); Pch = strtok (NULL, ":"); Bar = Molkok (Stellen (pH)); Strakpy (bar, pc); Return; } Int main (zero) {Charlie * Mayabaz, * Mafoo, * Mayurbar; Mybaz = "Hello: world"; FN (Maybus, Mayfu, Maybar); Fprintf (stadder, "% s% s", Mayfoo, Maybar); }  

UPDATE is an updated version with some suggestions for implementation:

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; #include & lt; String.h & gt; Define # MAXLINE 1024 zero fn (char * baz, char ** foo, char ** times) {char line [MAXLINE]; Char * pch; Strakpy (line, falcon); Pch = strtok (line, ":"); * Foo = (four *) mlok (strlen (pch) +1); (* Foo) [Stellen (PC)] = '\ n'; Strcpy (* foo, pch); Pch = strtok (NULL, ":"); * Bar = (four *) Molec (strlen (pch) + 1); (* Times) [Stellen (pc)] = '\ n'; Strcpy (* times, pch); Return; } Int main (zero) {Charlie * Mayabaz, * Mafoo, * Mayurbar; Mybaz = "Hello: world"; FN (Maybus, and Mifu, and Maribor); Fprintf (stadder, "% s% s", Mayfoo, Maybar); Free myfoo; Free mybar; First thing, those MLX should be for  strlen (whatever) + 1  bytes is a 0 character to indicate the end of the string, which is called the NUL terminator, and it is measured by strangle Not included in length known. 

The next thing, Strontok modifies the string you are searching for. You pass it to an indicator in a string, which you are not allowed to modify (you can not really modify the string). This may be the reason for segfault, instead of using an indicator to use the non-modifiable string literally, you can copy it into your own, modified buffer:

  char mybaz [] = "Hello: world";  

What this does is keep a size 12 character array on the heap, and copy the bytes of the string in that array. This works because the compiler knows, time compilation, how long the string is, and can make space accordingly. It saves using malloc for that copy.

The problem present in your context is that you are currently passing value of mybaz, myfoo, and mybar in your work until you pointer Myfoo and mybar do not pass the variable till you can modify the collar variable, since Maifu is a four *, there is an indicator for it four **:

  zero fn (char * Baz, char ** foo, char ** times) // points-to-pointers * foo = malloc (...); Set the price set by // set Fu FN (MyBus, and Mifu, and Mieber); // myfoo and mybar near points  

There is no effect on myfoo to modify the function in your code. myfoo is not inevitable, so if none of the first two things are causing it, then you are most likely to get printed using that undefined indicator.

This is working basically, you might want to add some error-management. Stroke can return Neil if he does not find that separator, and you can not call strlen with a tap. malloc If there is not enough memory, you can return zero, and you can not call strcpy with a tap too.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -