if i have a simple struct such as How would i got about dynamically allocating memory for this struct using malloc?
struct Dimensions{
int height, width;
char name;
};
I um unsure on how to go about this, I have tried
struct Dimension* dim = malloc(sizeof(struct Dimensions));
Also I would like then access the height and width variable in a loop later on in my code. My first thought would be to use a pointer but im unsure on what this would exactly be.
Would it be something like
int h = *width
I'm very new to C. Thanks
dim->height,dim->widthanddim->name--dimis a pointer so you use the->operator to reference members. Ifdimwere NOT a pointer, but a declaration of typestruct Dimensionitself, then you would use the.operator to access the members.char namewould contain more than one character, you better change its definition tochar *name(a pointer, pointing to a collection of characters, sized dynamically) orchar name[20](an array of characters of fixed size).