138 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
3
votes
2
answers
196
views
C macro to compute array length that returns 0 for NULL
I am writing hardcoded calls of functions that takes array(s) in parameter, e.g.
int foo(size_t length, const int array[/* length */]);
In order to avoid the error-prone process of keeping track of ...
2
votes
1
answer
121
views
Initialize array of pointer to struct using compound literals without being verbose
I would like to initialize a NULL terminated array of pointer to struct using compound literals.
Currently, I achieve this by:
struct object
{
int data1;
char data2;
};
struct object *...
5
votes
2
answers
110
views
The question about lifetime of compound literal which is associated with struct member
Check following contrived code:
#include <stdio.h>
typedef struct {
void *ctx;
} A;
int main(void) {
A b;
{
A a = {&(int){3}};
printf("%p\n", a.ctx);
...
4
votes
1
answer
87
views
Is returning compound literal from generic selection safe?
Check following contrived code:
#include <stdio.h>
void print_int(int *p) {
printf("%d\n", *p);
}
void print_double(double *p) {
printf("%f\n", *p);
}
#define ...
1
vote
2
answers
158
views
Are compound literals always lvalue?
I was reading C Notes for Professionals, in which, it claims that Compound Literals can only be lvalues. But LLMs have mixed answers upon that stating it could depend upon the context and could be ...
1
vote
1
answer
87
views
Typing of Array in C structure is "overflowing" into subsequent fields?
I have the following types:
typedef float vec4[4]; // from cglm
typedef struct Node Node;
struct Node {
float expand;
vec4 color;
Node *children;
}
For some reason, the color field's ...
2
votes
1
answer
151
views
array of pointers to pointers to integers VS pointer to pointer to array of integers
I have 3 integers A, B & C from which I make pointers that I group in an array arrayABC of pointers to pointers:
int A = 1;
int B = 2;
int C = 3;
int *ptA = &A;
int *ptB = &B;
int *ptC = &...
1
vote
2
answers
171
views
How to set a pointer to an integer in a compound literal that is initialized in a function?
I tried to initialise a structure via a compound literal in a function. After solving a part of my problem (see Use of compound literal inside a function to init a variable in C), I have a new problem ...
1
vote
2
answers
114
views
Use of compound literal inside a function to init a variable in C
This question follows this one : Is there a syntax in C to DIRECTLY INIT a struct member that is a const array of constant literal?.
Eric Postpischil gave me a solution using compound literals.
Now, I ...
2
votes
1
answer
102
views
Why does return of a compound literal (C99) generate more assembly code?
I have two functions that return a struct, one returns it via compund litearal and the other initializes a variable to then return it:
typedef struct {
int storage[256];
} data_t;
data_t ...
2
votes
2
answers
155
views
Declaring flexible arrays as constants
I'm trying to declare some constant data structures in my code, ideally without having to declare subelements (this is a follow-on from a previous question about multiple lists of strings: different ...
1
vote
3
answers
289
views
Can I declare an array of different length arrays in C without variable names?
I am trying to figure out if it's possible to do the following, but without having to declare the individual variables...
static char *set_list1[] = { "set", "one", NULL };
static ...
0
votes
1
answer
80
views
Weird macros for defining constants
I stumbled on the following stuff in someone's source code in C:
typedef struct {
u32 reg;
} reg_t;
#define _REG(r) ((const reg_t){.reg=(r)})
#define REG_A _REG(123)
#define REG_B _REG(456)
...
...
2
votes
2
answers
153
views
Does the C11/C17 Standard allow the compiler to clobber compound literals' memory?
I've found compound literals a very useful and elegant way to send initialized arrays and structs to functions without writing overly verbose code, but I want to understand the cost of writing code ...
1
vote
1
answer
264
views
Why can GCC not handle compile-time evaluation of compound literal in a ternary with sizeof as a condition inside a struct initializer?
The following code:
struct Int {
int i;
};
const struct Int i = {sizeof(int) ? (int){1} : 0};
Results in:
initializer element is not constant
(Live demo GCC)
Even though those statements are at ...