malloc used to allocate memory. You can use the pointer either by highlighting it with malloc or by pointing to an already allocated part of the memory.
In the first example that you specified, if you did not specify a pointer to an address, it does not stand out and cannot be used. For example, you can indicate that it points to the current value of int:
int value = 0; int* pointer; pointer = &value;
But you cannot assign a value to it:
int value = 0; int* pointer; *pointer = value;
This is your second case.
calloc is basically malloc + initialization.
Change Regardless, this is not a good example of using malloc. Best used probably when you need to allocate an array of variable size (unknown at compile time). Then you will need to use:
int* array = (int*)malloc(N * sizeof(int));
This is useful for two reasons:
- If N is a variable, you cannot perform static allocation, for example
int array[N]; - The stack may be limited by the amount of space that you can allocate.
Tudor source share