I'm trying to assign a specific value of char array (or string) through conditionals depending on the value of char.
Suppose c is a char declared before the code with a specific value of some sort.
char fruit[];
if (c == 'R') {
fruit[] = "Red";
}
else if (c == 'Y') {
fruit[] = "Yellow";
}
else if (c == 'G') {
fruit[] = "Green";
}
else if (c == "B") {
fruit[] = "Blue";
}
This code is obviously wrong but should give an idea on what I'm trying to do.
I am either planning to use a correct version of this for a simple program or have to go through around four times more conditionals to manually print the said string values which would be an immense pain.
char fruit[];andfruit[] = "Red";is not valid. You need to takechar *fruitand thenfruit = "Red";but this is string literal not modifiable. OR you can usechar fruit[100]thenstrcpy(fruit,"Red").else if (c == "B")is incorrect,also usestrcpyfor copying strings.