Originally Posted by
thachdovan
Dear @
GulluButt,
Thanks for your reply, I put at the end, but still error. please give me some suggestion
It should work. I have just made changes as I said in previous answer.
Code:
#include <stdio.h>
#include <stdlib.h>
int *p_array;
void initArr(int size){
p_array = (int *)malloc(sizeof(int)*size);
}
void storeData(int size){
p_array = realloc(p_array, sizeof(int)*size);
int idx;
for(idx = 0; idx < size; ++idx){
p_array[idx] = idx;
printf("%d\n",p_array[idx]);
}
}
int main(void) {
initArr(5); //OK
storeData(5); //!
storeData(8); //!
free(p_array);
return EXIT_SUCCESS;
}
Here is my Output that clang Compiler Outputs
Code:
0
1
2
3
4
0
1
2
3
4
5
6
7
As a side note you do not Need to write (int *) when malloc and realloc