like


....
CString name[5];
for(int i=0;i<5;i++)
name="Jack";
....


is this the right way?

Dani AI

Generated

Both per-element assignment and a brace-list at declaration are valid ways to populate a fixed C-style array of MFC/ATL CString (as shown earlier by and ; is correct that those approaches work). A few practical clarifications and alternatives that matter in real projects:

A plain CString name[5]; default-constructs five empty CString objects, so assigning later is safe; CString itself is in the MFC/ATL headers—see the official reference for details on constructors and headers: .

If you want a more flexible or modern approach (resizing, easier initialization), prefer standard containers or MFC arrays. Examples that avoid manual index management:

#include <vector>
#include <afxstr.h>   // or <atlstr.h>

std::vector<CString> names(5, _T("Sample"));   // fill constructor
#include <array>
#include <afxstr.h>

std::array<CString, 5> names;
names.fill(_T("Sample"));    // C++11+

For MFC-specific dynamic arrays use CStringArray when you need typical MFC collection behavior; see . Standard container docs: std::vector and std::array.

Practical notes: use _T("...") or wide-string literals consistently when building with UNICODE; include the correct header (<afxstr.h> for MFC, <atlstr.h> for ATL). For code that must resize at runtime, prefer std::vector or CStringArray over a fixed CString[].

Recommended Answers

All 2 Replies

It does the trick

Here is another way

CString array[5] = { "Jack","Jim","Jerry","Judy","Ralph"};
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.