1

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 = XMLString::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

1
  • please indicate where in the code your problem manifests it self? Commented Oct 14, 2011 at 13:41

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.