So I've got "m_starts" as a pointer to an int (int * m_starts;).
And a const char * of "m_str".

m_str = "bad,cold,new".

How can I allocate "m_starts" to store the starting index of each word in m_str???

For loop?

Recommended Answers

All 10 Replies

First you have to find out how many comma-separated words are in the string. Iterate through the string and count the commans. Then allocate the int array large enough to hold that many integers. After that, go through the string agsin, this time when you find a comma set one of the integers to be the numeric value of where the comma was found.

For example in the string you posted you will need three integers because the string contains three comma-separated words. The first integer will be 0, the second integer 4 and the 3d integer is 9.

Understood, but what I'm asking is how do i allocate it to the size i want (in this case 3)??

I mean I can set "m_starts[3];" or even run through a for loop so i leave it open to any number of sizes, but what I DON'T understand is how to then use it to store the starting index of each word in m_str. How do i go about that process??

>>how do i allocate it to the size i want
This is c++ so use the new operator to allocate the array m_starts = new int[NumberOfIntegers];

dont forget to use delete to deallocate the memory. :)

But I though DMA is for integers contain in an array only? Characters won't work for DMA....I've tried it before

But I though DMA is for integers contain in an array only? Characters won't work for DMA....I've tried it before

Huh? I assume DMA means Direct Memory Address. How does that relate to the discussion of this thread ? BTW DMA is not possible with any 32-bit compiler unless writing a kernel-mode device driver.

Huh? I assume DMA means Direct Memory Address. How does that relate to the discussion of this thread ? BTW DMA is not possible with any 32-bit compiler unless writing a kernel-mode device driver.

I think he means 'Dynamic Memory Allocation' - I could be wrong though. Either way, it works fine with both char and int

My mistake...DMA - Dynamic Memory Allocation.

But I though DMA is for integers contain in an array only? Characters won't work for DMA....I've tried it before

Now that I know what you mean by DMA -- it works with any data types, either simple or complex. If you couldn't make it work then its your fault not that of the language.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.