I am building a hash library, this library works with different structs and all those structs haves an unsigned type as first member, an example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct data {
    unsigned hash;
    void (*func)(struct data *);
};
struct another_data {unsigned hash; int value;};
static void *hash_insert(const char *text, size_t size)
{
    unsigned *hash;
    hash = malloc(size);
    // *hash = hash(text);
    *hash = (unsigned)strlen(text);
    return hash;
}
static void func(struct data *data)
{
    printf("%u\n", data->hash);
}
int main(void)
{
    struct data *data;
    data = hash_insert("Some text", sizeof *data);
    data->func = func;
    data->func(data);
    free(data);
    return 0;
}
Since the first member of the struct and the struct itself haves the same alignment requirements, is it valid to call malloc with a pointer of the type of the first member in order to reserve space for the entire struct?
unsigned *hash = malloc(size); /* where size is the size of the struct */
EDIT:
In this related question provided by @MohitJain:
struct Msg
{
   unsigned int a;
   unsigned int b;
};
...
uint32_t* buff = malloc(sizeof(Msg));
// Alias that buffer through message
Msg* msg = (Msg*)(buff);
The strict aliasing rule makes this setup illegal
But in my case I am returning a void * from the function, I can use this (returned) pointer inside main without alignment issues, is this assumption correct?
 
     
     
     
     
     
    