0

I want to declare an array of type (char / string) confused! dunno which one.. which will store 5 names
this code couldnt work
char names[5]={monitor,cpu,mouse,joystick,system};
someone guide me please

Edited by deceptikon: Moved to C++

2
Contributors
1
Reply
13
Views
3 Years
Discussion Span
Last Post by deceptikon
0

My recommendation would be to use the string class:

string names[] = {"monitor", "cpu", "mouse", "joystick", "system"};

The problem with your code is that the "strings" aren't string literals, so they're treated as identifiers that don't exist. Further, the type of your array is char, which means each element of the array can hold one character and no more.

To hold multiple characters, the type of the array needs to be a char* that points to a string literal or dynamic memory, an array of char (ie. a 2D array), or an array of std::string as in my example above. The latter provides the most flexibility by far, and that's why it's recommended.

Edited by deceptikon

Votes + Comments
thank you it worked perfectly
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.