Allocating memory for a Structure in C

c, malloc, structure

Solution

My favorite:

#include <stdlib.h>

struct st *x = malloc(sizeof *x); 

Note that:

- `x` must be a pointer

- no cast is required

- include appropriate header

Problem

I'm tasked to create a program which dynamically allocates memory for a structure. normally we would use ``` x=malloc(sizeof(int)*y); ``` However, what do I use for a structure variable? I don't think its possible to do ``` struct st x = malloc(sizeof(struct)); ``` Could someone help me out? Thanks!

Original source