I have code to dynamically allocate an array of 100 doubles to a pointer and initialize the memory with values from 1.0 to 100.0.
My question is, in the code below why don’t I have to write *ptr++ at each iteration of the for loop. I’ve tried it and it doesn’t work.
void allocate_array(){
double *ptr;
ptr= (double*)malloc(sizeof(double)*100);
int i=0;
float j=0.0;
for(i=0;i<100;i++){
*ptr=j++;
printf(" %0.1lf\n",*ptr);
}
}
Well, you don’t have to do anything. In your code, you don’t initialize the whole array – you just assign 100 different values to its first member. Your code is just like:
If you want to initialize the whole array, do something like: