I am trying to create an array of buffers. I need to store an integer into each buffer. I’m not quite sure how this should be done.
int BUFFER_LENGTH = 50; //the size of each buffer
int numberOfBuffers = 10; //number of buffers
int *pBuffers; //array of buffers
pBuffers = (int *) calloc (numberOfBuffers, sizeof(int)); //make array size of numberOfBuffers
int i;
for (i = 0; i < n; i++){ //initialize each buffer to zero.
&pBuffers[i] = 0x00;
}
What is it that I am doing wrong? This code isn’t really working.
You might want to allocate enough space. Right there you only allocate enough space for 10 ints; looks like you want to allocate enough for 500. The simple way is
int buffers[10][50]. But if you want to calloc, you have tocalloc(BUFFER_LENGTH, sizeof(int))numberOfBufferstimes.Also, calloc automatically clears the allocated memory, so no need to do that.