Vorrei allocare una struttura sull'heap, inizializzarla e restituire un puntatore ad esso da una funzione. Mi chiedo se c'è un modo per me per inizializzare i membri const di una struttura in questo scenario:Come inizializzare i membri const delle strutture nell'heap
#include <stdlib.h>
typedef struct {
const int x;
const int y;
} ImmutablePoint;
ImmutablePoint * make_immutable_point(int x, int y)
{
ImmutablePoint *p = (ImmutablePoint *)malloc(sizeof(ImmutablePoint));
if (p == NULL) abort();
// How to initialize members x and y?
return p;
}
Devo concludere da questo che è impossibile per allocare e inizializzare una struct sul mucchio che contiene i membri const ?
Si noti che l'uso di .x e .y nell'inizializzazione di init è C99, ovviamente è possibile utilizzare voci unamed se il compilatore non lo supporta. – Trent
Brillante, caf - grazie! – user268344
+1 Per il chiarimento su 'malloc' tra C e C++. –