using malloc to allocate pointers

If you want to allocate a integer my understanding is that you do this:

int* x = (int*) malloc(sizeof(int));

If I understand correctly malloc will allocate an integer on the heap and return a pointer to it which will then be stored in x. If I wanted to allocated an int* instead would I do this?

int** x = (int**) malloc(sizeof(int*));

In general, does malloc return a point to the type that it just created? When you do this it looks like you are casting the pointer that malloc returns but not casting the actual memory that malloc allocated. Why is that?

链接地址: http://www.djcxy.com/p/86504.html

上一篇: C中的堆大小限制

下一篇: 使用malloc分配指针