c - Tricky pointer aliasing issue -
The following code fails to compile because the complainant complains about the four **
Does PrintStrings ()
. If I declare the strs
variable as const char **
, call PrintStrings ()
works, but Memcpy ()
and free ()
calls then complain that they are receiving const type anyway around this issue? Note that I do not want to enter strs
in PrintStrings ()
incompatbiele type
#include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; #include & lt; String.h & gt; Zero print saturation (cons four * cons * strings) {int x = 0; While (strs [x]) printf ("% s", strs [x ++]); } Int main (zero) {int x = 0; / * If I do this "const char **" then the memory and free will fail, but if I close the const, then call printstring () call * / char ** strs = malloc (5 * sizeof (char) *)); Struts [0] = molk (128); Memcpy (strs [0], "Hello", 1 + strlen ("Hello"); Strs [1] = Mallok (128); Memcpy (strs [1], "", 1 + strlen ("")); Strs [2] = Molkok (128); Memcpy (strs [2], "World!", 1 + strlen ("World!"); Strs [3] = Molkok (128); Memcpy (strs [3], "\ n", 1 + strlen ("\ n")); Strs [4] = null; PrintStrings (STR); While (strs [x]) free (strs [x ++]); Free (STR); Return 0; }
[edit]
Please remove the duplicate marker with this question. I fully understand why the artist is invalid, and unlike the other posters I am asking about. It is true that both the questions and the other questions center around the same compiler issue, but the second question is asking why the compiler does the first place, while I am asking for a solution in a specific and difficult matter.
Change your code so it's like this:
Four Constances * * Strs = malloc (sizeof (four *) * 5); Four * s; Strs [0] = s = malloc (128); Mempi (S, "Hello", 1 + stroll ("Hello"); Strs [1] = s = malloc (128); Memcopy (S, "", 1 + strollon ("")); ... while (strs [x]) free ((* four *) strs [x ++]);
Comments
Post a Comment