There are several types of confusion here. A pointer points to memory (duh), but you need to get that memory from somewhere. How you initialize that memory is separate.
You can allocate your structure on the stack by declaring a local variable inside your function:
// Initialize all buff elements and index to 0
// Note: Buffer, not Buffer*
Buffer newBuf = { {0}, 0 };
Or you can allocate it on the heap:
Buffer *newBuf = malloc(sizeof(Buffer));
memset(newBuf, 0, sizeof(Buffer));
When allocating objects on the stack, they are only valid while the current function is executing. You can't return a pointer to an object on the stack. Further, stack space is typically limited, so you can't put megabyte-sized objects there. When allocating memory on the heap with malloc()
, you need to take care to free()
when it is not used any more, otherwise you leak memory.
You see that when allocating objects on the stack, you are able to use an initializer-list { {0}, 0 }
. In the heap case, you can not do that and you have to zero the memory manually using memset.