I want to assign string to char array
here is code -
char *resultArray[100];
int cnt = 0;
void MySAX2Handler::startElement(const XMLCh* const uri, const XMLCh* const localname,
const XMLCh* const qname, const Attributes& attrs)
{
char* message = XMLS开发者_运维知识库tring::transcode(localname);
resultArray[cnt] = message;
cnt++;
for (int idx = 0; idx < attrs.getLength(); idx++)
{
char* attrName = XMLString::transcode(attrs.getLocalName(idx));
char* attrValue = XMLString::transcode(attrs.getValue(idx));
resultArray[cnt] = attrName;
cnt++;
resultArray[cnt] = attrValue;
cnt++;
}
XMLString::release(&message);
}
after iterating through resultArray, its printing some garbage values
attrName
and attrValue
are both pointers and char
is a type of integer, so the assignment operator will simply copy the value of the pointer (the address), not the content.
Either loop through the strings or use some version of strcpy()
, or indeed one of the C++ string libraries.
First off, when you declare your array, you should use
char *resultArray = new ResultArray[100];
vs
char *resultArray[100];
Next, you shouldn't ever use a loop to fill up a bounded array without knowing the limits of the loop. You will overflow your array, and cause a segfault. Unless of course you know your length won't reach close to 100. (still a bad idea, but to slap something together quickly, I won't complain)
Without more information, I can't tell you why you're getting garbage. How are you printing these out? What do those 'transcode' functions do? Where is the data coming from?
PS - remember you can use
resultArray[cnt++] = attrName;
resultArray[cnt++] = attrValue;
精彩评论