1

I have character array like

char *a[]={"hi","hello","how are you"};

I want to convert it into CStringArray

How can I do this?

4
  • Post what you have tried? Commented Mar 8, 2016 at 8:46
  • Avoid any CContainer of MFC and use std::containers. Commented Mar 8, 2016 at 8:56
  • It should be const char *a[] Commented Mar 8, 2016 at 9:00
  • @AnoopLL I know the way to convert character array to CString but I want to convert it into CStringArray Commented Mar 8, 2016 at 9:16

1 Answer 1

2
char *a[] = { "hi","hello","how are you" };
CStringArray array;

for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
{
    array.Add(a[i]);
}

sizeof(a) / sizeof(a[0]) is the number of string literals in the a array, that is 3.

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

10 Comments

It gives me error saying `INT_PTR CStringArray::Add(LPCTSTR)' : cannot convert parameter 1 from 'char *' to 'LPCTSTR'
@MahekShah strange, it compiles here. What is your OS, compiler, IDE etc. ?
M using visual studio on windows 7.
@VladFeinstein, yes they do, you must download an addon from Microsoft. The URL is specified in the error message displayed by VS when you try to compile an MBCS program without the addon installed. They added this soon after VS2012 came out, because there is still tons of legacy code out there which is being maintained.
Proposing to use MBCS for new code is questionable. Just because Microsoft released a package supporting MBCS for MFC does not mean, one should be using it without a striking reason. The array should be declared constant as well: const char* const a[] ....
|

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.