compound-literals

C compound literals, pointer to arrays

泄露秘密 提交于 2019-11-28 18:14:15
I'm trying to assign a compound literal to a variable, but it seems not to work, see: int *p[] = (int *[]) {{1,2,3},{4,5,6}}; I got a error in gcc. but if I write only this: int p[] = (int []) {1,2,3,4,5,6}; Then it's okay. But is not what I want. I don't understand why the error occurrs, because if I initialize it like a array, or use it with a pointer of arrays of chars, its okay, see: int *p[] = (int *[]) {{1,2,3},{4,5,6}}; //I got a error int p[][3] = {{1,2,3},{4,5,6}}; //it's okay char *p[] = (char *[]) {"one", "two"...}; // it's okay! Note I don't understand why I got an error in the

What is the lifetime of compound literals passed as arguments?

折月煮酒 提交于 2019-11-28 13:34:11
This compiles without warnings using clang. typedef struct { int option; int value; } someType; someType *init(someType *ptr) { *ptr = (someType) { .option = ptr->option | ANOTHEROPT, .value = 1 }; return ptr; } int main() { someType *typePtr = init( &(someType) { .option = SOMEOPT }); // do something else with typePtr } Is this even valid C? If so: What is the lifetime of the compound literal? It's valid C in C99 or above. C99 §6.5.2.5 Compound literals The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound literal occurs outside

Why can't I pass constant arrays as arguments?

南楼画角 提交于 2019-11-28 00:57:22
In C, why can't I do this: arrayfn({1.0, 2.0, 3.0}); if arrayfn is some function that takes in one parameter of type double[] or double* , whichever. Trying this gives me a syntax error. Is there a way that I could achieve something in C like this - generating and immediately passing an array known at compile time - that avoids having to spend a line of code pre-declaring and filling it? Short answer: You need to make use of a compound literal . Something like arrayfn( (double[]) {1.0, 2.0, 3.0} ); should do the job. Some explanation Coming to the part, why arrayfn({1.0, 2.0, 3.0}); did not

C compound literals, pointer to arrays

强颜欢笑 提交于 2019-11-27 11:08:36
问题 I'm trying to assign a compound literal to a variable, but it seems not to work, see: int *p[] = (int *[]) {{1,2,3},{4,5,6}}; I got a error in gcc. but if I write only this: int p[] = (int []) {1,2,3,4,5,6}; Then it's okay. But is not what I want. I don't understand why the error occurrs, because if I initialize it like a array, or use it with a pointer of arrays of chars, its okay, see: int *p[] = (int *[]) {{1,2,3},{4,5,6}}; //I got a error int p[][3] = {{1,2,3},{4,5,6}}; //it's okay char

Why are compound literals in C modifiable

你说的曾经没有我的故事 提交于 2019-11-27 09:42:24
One does usually associate 'unmodifiable' with the term literal char* str = "Hello World!"; *str = 'B'; // Bus Error! However when using compound literals, I quickly discovered they are completely modifiable (and locking at the generated machine code, you see they are pushed on the stack): char* str = (char[]){"Hello World"}; *str = 'B'; // A-Okay! I'm compiling with clang-703.0.29 . Shouldn't those two examples generate the exact same machine code? Is a compound literal really a literal, if it's modifiable? EDIT: An even shorter example would be: "Hello World"[0] = 'B'; // Bus Error! (char[])

What is the lifetime of compound literals passed as arguments?

空扰寡人 提交于 2019-11-27 07:45:11
问题 This compiles without warnings using clang. typedef struct { int option; int value; } someType; someType *init(someType *ptr) { *ptr = (someType) { .option = ptr->option | ANOTHEROPT, .value = 1 }; return ptr; } int main() { someType *typePtr = init( &(someType) { .option = SOMEOPT }); // do something else with typePtr } Is this even valid C? If so: What is the lifetime of the compound literal? 回答1: It's valid C in C99 or above. C99 §6.5.2.5 Compound literals The value of the compound literal

How to use compound literals to `fprintf()` multiple formatted numbers with arbitrary bases?

跟風遠走 提交于 2019-11-27 05:20:56
I'd like to convert multiple numbers into some representation and then use the flags, width and precision of *printf() specifiers. Preference would be to avoid global or static buffers. The key problem appears to be is how to provide a char[] for each of the converted numbers? fprintf(ostream, "some_format", foo(int_a, base_x), foo(int_b, base_y), ...); How to use C11 compound literals to solve this? How to use C99 (or later) compound literals to solve this? chux C99 C11 introduced compound literals which allow not only a complicated initialized structure, but also an "in-line" variable. Code

Are compound literals Standard C++?

随声附和 提交于 2019-11-27 04:48:50
Compound Literals are a C99 construct. Even though I can do this in C++ : #include <iostream> using namespace std; int main() { for (auto i : (float[2]) {2.7, 3.1}) cout << i << endl; } It seems that for example MSVC supports it as an extension . Yet all compilers I can get my hands on, compile the above mentioned code. So is this a feature available in C++14 ? Is there a different standard term (It looks to me like just creating a temporary using braced initialization) ? Side Note : "Compound Literals" (or whatever I should call the above) are a pack expansion context ( just to mention a

Where does the compound/string literals get stored in the memory?

陌路散爱 提交于 2019-11-26 21:24:02
问题 I read that ; A compound literal is a C99 feature that can be used to create an array with no name. Consider the example: int *p = (int []){3, 0, 3, 4, 1}; p points to the first element of a five- element array containing 3, 0, 3, 4 , and 1 . Actually I want to know, Is this array will stored in memory or not as it doesn't have a name? In other words in case of char* str = "hello" Where the string "hello" will stored in memory? 回答1: C 2011 (N1570) 6.5.2.5 5 says: If the compound literal

How to use compound literals to `fprintf()` multiple formatted numbers with arbitrary bases?

回眸只為那壹抹淺笑 提交于 2019-11-26 11:32:03
问题 I\'d like to convert multiple numbers into some representation and then use the flags, width and precision of *printf() specifiers. Preference would be to avoid global or static buffers. The key problem appears to be is how to provide a char[] for each of the converted numbers? fprintf(ostream, \"some_format\", foo(int_a, base_x), foo(int_b, base_y), ...); How to use C11 compound literals to solve this? How to use C99 (or later) compound literals to solve this? 回答1: C99 C11 introduced