Say if i want to print out my integer stored inside a “array” , which indeed a pointer in memory.When i use the code:
int main(void)
{
int *arr = malloc(3*sizeof(int));
int *p = arr;
*arr++=1;
*arr++=2;
*arr=3;
while (??) // what should be filled in the while?
printf("%d",*p); // so that we can get the validly stored elements?
return 0;
}
C does not provide a built-in way to find the size of dynamically allocated chunks of memory. You have to store the size, and pass it “on the side”.
One way to address this would be to create a
structcombining a pointer and asize_tvalue describing the number of elements allocated in the array.