how to make a subarray?

G

Guest

Writing my own algorithm to make a subarray in Visual C++ would be easy enough, but is there a useful function or method for doing this? I have strings stored as TCHAR[] and I want to get substrings of those, but the basic String.substr() function doesn't compile on that. I tried this

TCHAR string[size]
TCHAR substring[size]
//function that returns a value for strin
substring = string->Substring(3,3)

and I get this when I compile
error C2227: left of '->Substring' must point to class/struct/unio

Thanks in advance!
 
T

Thobias Jones

In C++, you cannot assign a value to an array variable. In this case,
you are trying to assign a value to substring, which is illegal.

What you need to do is an element-by-element copy into the substring
array. So you might need something like:

for(int ii = 0; ii < 3; ii++)
substring[ii] = string[ii + 3];
substring[3] = 0;

This will copy 3 characters into substring, starting at index 3 in
string. The last line will null-terminate the string, since it needs to
be in order to be a valid string.

The other option is to use the C++ string class library, instead of
TCHAR arrays.

Thobias Jones
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top