On C programming language, I have encountered several ways to allocate memory for a data structure:
#include <stdio.h>
#include <stdlib.h>
typedef struct Element
{
int number;
struct Element *next;
}Element;
int main(int argc, char *argv[])
{
/* Method NO.1 */
Element *ptr1 = malloc(sizeof(*ptr1));
/* Method NO.2 */
Element *ptr2 = malloc(sizeof(Element));
/* Method NO.3 */
Element *ptr3 = (Element*) malloc (sizeof(Element));
return EXIT_SUCCESS;
}
There are no compilation errors.
But I got confused, so what is the difference beteween them, and which one should be preferred?