Several answers say that you can only use the address of named variables, but this is not entirely correct: if you use C99 or higher, you can also take the address of a complex literal or field of a complex literal. This is usually useful for, for example, calling a function that takes a pointer to some structure that needs to be created only at the time of the call. Example: draw(&(struct point){ 5, 10 });
Some ways in which this could be used to obtain the address of the scalar [edited with explanations]:
And for the duration of the containing block, * c will be equal to 'a'.
However, a terminator and a more general way of getting an address into a literal using compound literals is to simply declare an array literal with only one element, which, as usual, will decay to a pointer:
char *c = (char[]){'a'};
And this is pretty typical syntax for a job. But, as it turned out, the language allows us to do something even more direct and slightly unintuitive: we can declare a compound literal of a scalar type. Which reduces all of the above to the more obvious:
char *c = &(char){'a'};
In your case
#include <stdio.h> int main(int argc, char const* argv[]) { char *str[3]; str[1] = &(char){'a'}; return 0; }
This is a little more verbose than '&' and in fact just a few keys are less than assigning a temporary variable, but there it is.
source share