Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary operation with a string output in C

Can someone explain why this simple ternary operation won't even compile, in C?

void main(int argc, char *argv[]){
    int a = atoi(argv[1]);
    char foo[] = (a == 1) ? "bar1" : "bar2";
}

It seems to be a problem with strings in particular.

like image 787
Pier-Alexandre Bouchard Avatar asked Oct 02 '12 16:10

Pier-Alexandre Bouchard


People also ask

Can ternary operator return string?

Ternary operator values The values part of the ternary operator in the above example is this: “This is an even number!” : “This is an odd number!”; In the example above, if the condition evaluates to true then the ternary operator will return the string value “This is an even number!”.

What is the output of ternary operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

Can we use printf in ternary operator?

We use the ternary operator in C to run one code when the condition is true and another code when the condition is false. For example, (age >= 18) ? printf("Can Vote") : printf("Cannot Vote");

What is a ternary operator in C give an example?

It helps to think of the ternary operator as a shorthand way or writing an if-else statement. Here's a simple decision-making example using if and else: int a = 10, b = 20, c; if (a < b) { c = a; } else { c = b; } printf("%d", c); This example takes more than 10 lines, but that isn't necessary.


1 Answers

A string literal "bar", when used in an expression (in this case, inside the ternary operator), is a pointer to pre-allocated memory. You cannot initialize an array using a pointer to data, only using a literal ("..." or {...}).

Instead, you could assign it to a char *:

const char *foo = (a == 1) ? "bar1" : "bar2";

This will not make a copy of the literal but point to it, so you should not modify the elements of foo. If you need a copy, you can use memcpy, provided that you know how big to declare the array:

char foo[5];
memcpy(foo, (a == 1) ? "bar1" : "bar2", sizeof foo);

If you particularly want to be able to assign the contents, then there is a trick to doing that; it is possible to implicitly copy the contents of a struct using assignment (as well as returning it from a function, and so on), no matter what the struct contains, and you could put a character array in a struct:

typedef struct { 
  char contents[5];
} mystring;

mystring foo = (a == 1) ? (mystring){"bar1"} : (mystring){"bar2"};
// You can also use assignments.
foo = (mystring){"baz"};

Just like the second option, when you do this you have to pick a fixed size for the array in the struct declaration. If you have strings of unbounded length, then you must use pointers.

like image 152
Kevin Reid Avatar answered Sep 18 '22 14:09

Kevin Reid