0
Pointer in C
Hi everyone. I really need some help on understanding pointer. Iâve been encountering this same question over and over in the C challenges and I really donât get it. Can anybody please help and explain how it works?đ„ Hereâs the question. ========================== What is the output of the following code? int *p = (int *)malloc(sizeof(int)*4); *p = 42; *(p + 2) = 43; int* v = p; *(v + 1) = 44; printf(â%dâ, p[1]); printf(â%dâ, v[0]);
3 RĂ©ponses
+ 3
// memory allocation to pointer variable p
int *p = (int*)malloc(sizeof(int)*4);
// set value of address pointer p as 42. it same with p[0]=4
*p = 42;
// same as above but its about p+2 (p[2])
*(p + 2) = 43;
// copy p's address to v. now they points same address.
int* v = p;
// set value of address pointer v+1 as 44. it same with p[1]=44 and v[1]=44
*(v + 1) = 44;
//output section
printf(â%dâ, p[1]);
printf(â%dâ, v[0]);
Hope this helps
+ 1
Mystika L Ohhh now Iâm getting it! Thank you so much!!đ
0
Jay Matthews thanks Jay đ but like I mentioned I really want to know how the code works out! Especially the 4th line:
int* v = p;
what happens here?