char arrays and ternary operators in C

is it possible to change the value held by a char array using the ternary operator?

This is what i have tried, but the output is not what im expecting:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int a;
char str [10];

printf("Enter a word:\n");
scanf("%s",&str); // user enters a word here, say the word "bad".

printf("Enter a number:\n");
scanf("%d",&a);


printf("%s\n",str);
// This is were im having the problems, im getting a compile error for incompatible types
str = a>1 ? "greater":"false";
printf("%s\n",str);



How can i get around this problem? I knew to C, and im trying to learn it before progressing to C++

Thanks

Dave
You cannot 'assign' arrays to each other.
You can assign pointers...

1
2
3
char* str;
...
str = (a > 1) ? "greater" : "less-than or equal";

To copy character arrays you must use a function, usually strcpy()

1
2
3
char str[ 10 ];
...
strcpy( str, (a > 1) ? "greater" : "false" );

Hope this helps.
Duoas,

thanks, i can remember seeingsomething about having to access arrys in a different way.

Thanks,

Dave
Topic archived. No new replies allowed.