how would i declare an array of namers? i would like to hold 10 names in one variable. how do i do that??
Printable View
how would i declare an array of namers? i would like to hold 10 names in one variable. how do i do that??
The easiest way is to declare an array of strings like so:Code:string names[10]={"Bob","Joe","Belzebub",etc etc};
but i would like to use 'char' insted of 'string' ...sorry i should've been alittle more specific in my first post.
I'd suggest a vector:Since you are using C Style strings then you would use const char* instead of string in PJYelton's example, but you wouldn't be able to modify the string values.Code:vector<string> names(10);
If you want to be able to modify the values, you could use a two dimensional array of char, or a one dimensional array of char* and use a loop with new to initialize each pointer with the character array.
This creates an array of an unspecified number of strings. The compiler will figure out how many strings by the number of strings in the initilizer list. If you want the array to have room for more later, then you will have to put a number between the brackets.
But if you are writing a c++ program, then normally use std::vector class. Occasionally C-style string arrays are easier, especially when they don't change, such as an array of month names, or weekday names.Code:const char* names[]={"Bob","Joe","Belzebub",etc etc};
thx dragon, your my hero.
Ancient Dragon's strings can't be modified.
Of course not -- that's what the const keyword is for at the beginning of the declaration. If you want a modifiable array it need to be something on the order of this (an array that will hold 10 strings and each string can be up to 254 characters + null terminator). change the numbers however you wish.Quote:
Originally Posted by dwks
there are several other ways to do this too, but the above is the simplest.Code:char array[10][255];