I am new to C and have a question about initializing a structure. I am using a structure that I have not created and so I don’t know what is inside of it. Therefore, I did not initialize it but the compiler complained. So I set it equal to NULL but I got a segmentation fault. Then I looked up how to set everything to 0 and it said to set it equal to {0}. That too gave me a segmentation fault. Since I know the function I am calling is correct, and that the array I pass it is of the correct size, I am almost certain it has something to do with the way I initialize the structure. The initialization is
struct aes_ctx *aes_struct = {0}
The header for the function is
void aes_setkey(aes_ctx *aes, const void *key, u_int len);
The way that I am calling it is
aes_setkey(aes_struct, aes, CCA_STRENGTH);
where aes is a buffer of size 16, CCA_STRENGTH is a constant int of 16.
To sum up the problem, I think that the way that I initialize the structure causes it to be unusable later on. Any help that can be given to me on this would be so appreciated!
Thanks!
You cannot initialize a pointer that way (well, you can, it just doesn’t point to anythign valid). Something like this is what you are after:
You can then pass the address of
aes_structto the function, but that depends on whether or not you need to dynamically allocate this thing (required scope and the size of the type will dictate this).So…
Or