How to convert a char array to a char pointer in C -


I have a piece of code that will give me a character array. I want to give this array in a function that expects a character indicator. When I enter this array in the function, I give the result of a partition fault in trying to run the program.

This works fine.

  char * buffer = "& gt; XML version = \" 1.0 \ "encoding = \" UTF-8 \ "? Gt; gt; & gt; sum & gt; & lt; section <55>   6  gt; part & gt; * & lt; / part & gt; & lt; / yoga & gt; & lt; / part & gt; Gt; "; Long output = calxaxml (buffer);  

When I try to create an array it will give me a split error

  char buffer [] = " gt; & lt; part & gt; 55 & lt; / part & gt; & lt; section & gt; 6 & Lt; / part & gt; & lt; part & gt; * & lt; / part & gt; & lt; / yoga & gt; "; Long output = calxaxml (buffer);  

In addition, when I try to create an indicator from the array, then the fault of the same clause happens.

  Four buffer [] = "& lt ;? Xml version = \" 1.0 \ "encoding = \" UTF-8 \ "? & Gt; & lt; yoga & gt; & lt; ; Part <55   6   <* *   

How can I solve it?

Edit: The function will pass XML parameters in the calcxML (char * xml) function xmlParseDoc () . This is the first task of a

  • because it is returning the address of a string literally. String literals are global, i.e. memory that holds the character, it is started because the program starts, and the location does not change.
  • Second should work, but there is very little information in working for it.
  • The third one is broken, it overwrites the memory assigned by malloc () with the address of a local array. Do not pay attention to the fact that it works, but as there is very little information in the case.

In addition to this, malloc () is not itself right in the third case.

It calculates strlen (buffer) * sized + 1 as the size, but since the ending character is also a character, then avoid it Does not mean that for allocation scaling, then it should be "to be" (strlen (buffer) + 1) * sizeof (char) . But again sizeof (char) is always 1, so scaling is completely unusable, that is, it should be just strlen (buffer) + 1 . Return value from malloc () is overwritten again, however, the code gets broken again.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -