在C编程语言分配内存的数据结构,我曾经遇到过多种方式为数据结构分配内存:不同的方式对C语言编程
#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;
}
有没有编译错误。
但是我很困惑,所以他们之间有什么区别,哪一个应该是首选?
和这里的**分配有什么不同? – Olaf