Question

So, in class we've been given this program in C:

int main() {
    int num;                                
    int *ptr; 
    int **handle;

    num = 14;                           
    ptr = (int *)malloc(2 * sizeof(int));   
    handle = &ptr;

    **handle = num;                     
    *(*handle+1) = num+1;                   
    *ptr = num-2;                           
    ptr = #                         
    *ptr = soma_ds(num-2)

What I understood from it was:

1.An integer, a pointer and a double pointer are declared

  1. num = 14; assings the value 14 to num

  2. ptr = (int *)malloc(2 * sizeof(int)); reserves in memory a block for a two-integer array assigned to ptr

4.Then handle = &ptr; assigns the value of the address of ptr to handle (meaning that handle points to ptr?)

5.And then **handle = num; confuses me, but I interpreted it as: the pointer to which handle is pointing (that would be ptr) is now pointing to num (14). Question: if this is correct, then what would be the effect of this operation, given that ptr is assigned the memory space of an array?

6.After this I'm pretty sure I'm getting all of it wrong. So the line *(*handle+1) = num+1 would mean, since handle has the address of ptr, that *handle+1 (*handle being ptr, therefore *handle+1 would be the increment of num's memory address) would be now pointing to num+1 (14+1=15). Question: if this is correct, would that mean that the array reserved to ptr is now composed by the integers 14 and 15 on a row?

7.Then *ptr = num-2;´; I assumed that would affect the variable num itself, working asnum = num - 2;` and therefore changing its value to 12. So, at this point, we'd have ptr pointing to the array composed of 14 and 15 and num having the value 12.

Question: I must be thinking wrong, because I can't understand what the next command, ptr = #, does. Would anyone be able to explain it/ provide a link or an example that would help me understand it?

Thank you!

Was it helpful?

Solution 2

"4. ... meaning that handle points to ptr?"

yes! ok.

"5. And then **handle = num; confuses me, but I interpreted it as: the pointer to which handle is pointing (that would be ptr) is now pointing to num"

Not correct: here there is the assignment of the value of num to (ie: num is copied into) the location pointed by the location pointed by handle. The location pointed by handle is ptr per the previous line, so num is copied into the the location pointed by ptr, that is the 1st of the two location allocated with malloc().

"6. ... would be now pointing to num+1"

the conclusion is correct, but you are not pointing to num+1. You have assigned/copied num+1 into *(*handle+1). Since *handle is ptr, into *(ptr+1) or if you prefer the effect is the same of ptr[1]=num+1 - where ptr is still pointing to the memory allocated with malloc().

"7.Then *ptr = num-2; I assumed that would affect the variable num itself, working asnum = num - 2;` and therefore changing its value to 12. So, at this point, we'd have ptr pointing to the array composed of 14 and 15 and num having the value 12.

nope: read "*" as "the object pointed by" - it's a good mnemonic. So you are still altering the object pointed by ptr, that is ptr[0] = num-2, you are changing again the first of the two integer locations allocated with malloc().

Only at the next line (ptr = #) you are changing the value of ptr to the address of num and operations on *ptr, the object pointed by ptr, are going to affect num now on.

OTHER TIPS

ptr will become the address of num i.e. where 14 is stored

5.And then **handle = num; confuses me, but I interpreted it as: the pointer to which handle is pointing (that would be ptr) is now pointing to num (14). Question: if this is correct, then what would be the effect of this operation, given that ptr is assigned the memory space of an array?

The above statement means you can access the num value using **handle
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top