Advertisement

strings and arrays?

Started by February 12, 2003 05:16 PM
3 comments, last by bilsa 21 years, 9 months ago
Hello! I''m trying to create an array with 20 strings, and would really appreciate if anyone could help me I need first to declare the array and then in the code allocate memory for it with "new char" or something. char* myStringArray; char myStringArray = new char[128]; Something like that. This doesn''t work, but of what I know that is how to do it? I would be glad if someone could show me how to defina an array and allocate memory to hold 20 strings and assign a couple of values to it. Thank you guys!

  #include <vector>#include <string>std::vector<std::string> myStringVector(20);myStringVector[5] = "This is a C++ string";  



  char* myArrayOfCStrings[20];myArrayOfCStrings[5] = new char[128];strncpy( myArrayOfCStrings[5], "This is a C string", 128 );myArrayOfCStrings[5][127] = ''\0'';...delete[] myArrayOfCStrings[5];  


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Thank you very much!
I like that C code
I dunno why, but even thought the vector example was shorter I like the longer one better^^

Anyway, thank you very much!
It is not C code, I used new and delete.

If you liked the second one better ... your loss.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote: If you liked the second one better ... your loss.


That''s a bold statement.

This topic is closed to new replies.

Advertisement