The following piece of code gives me the error: "too many initializers for 'char []'" :
int main()
{
int input;
char numbers[] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
std::cin >> input;
std::cout << ((input > 9) ? "Greater than 9" : numbers[input-1]) << std::endl;
}
What's needed for this to work is for numbers to be a pointer variable, i.e:
char * numbers[] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
I'm new to c++ and I'm trying to understand why this array needs to be a pointer and what exactly is happening in memory which requires this to be a pointer?
In other languages such as Java you can do the following:
import java.util.Scanner;
public class Playground
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
String[] numbers = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
System.out.println((input > 9) ? "Greater than 9" : numbers[input-1]);
}
}
No Pointers are required here, and from my understanding there's no need for them in this kind of scenario either.
Since the strings are literals, i.e. constants, you should declare the array like this:
const char* numbers[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
This is the C way of things with the array defined at compile time. So the memory required for the array is calculated by the compiler. Hence you have to declare it as an array of const char*, i.e. the contents can't be changed as it is defined at compile time.
You could declare the array like this:
char numbers[9][6] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
What this does is declare a 2D array of 9 elements, each element containing 6 chars. So now you could change the value of each word at run time if you want to. The square brackets [] make all the difference. When you use char*[] the compiler treats elements as literals.
You would have to allocate them on the heap if you don't know the size of each word in the array beforehand.
The C++ way is to use std:string (allocated on the heap internally for you) and these can be changed at run time:
std::string numbers[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
As @Max Langhof said in the comment
char is a single character. You are trying to initialize an array of characters with an array of strings
You mention using C++, so instead of using C style arrays [] use the std::array available in the array header file. Along with std::string
std::array<std::string,SIZE> = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
Declaring it as a pointer it essentially creates an array of char pointers which in plain english translates to an array of char arrays.

If you want to learn more about C style char arrays The Definitive C++ Book Guide and List
Take the following code
const char* a [] = {"sadas", "dasdas"};
std::cout << a[0][0]; // will output 's'
a[0] will return sadas
Having a pointer to a char array will make the value const (read-only)
a[0][0] = 'b' // illegal
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With