0

I doesn't understand the 1st line. What output is to come?

int*arr=(int*) malloc (3*size of(int)); arr[0]=1; arr[1]=3; arr[2]=2; printf ("%d", [arr[0]]);

11th Sep 2020, 1:59 PM
Suparna Podder
Suparna Podder - avatar
5 Respostas
+ 2
allocates requested size of bytes and returns a void pointer pointing to the first byte of the allocated space please read this pos you can learn pointers concepts https://www.design-reuse.com/articles/25090/dynamic-memory-allocation-fragmentation-c.html Since the size of int is 4 bytes, this statement will allocate 3*4 bytes of memory. And, the pointer *arr point the first byte in the allocated memory
11th Sep 2020, 5:02 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Suparna Podder Here arr[arr[0]] means here arr[0] is 1 so arr[0] will give 1 so arr[1] and its value is 3 .becoz(arr[1]=3) { int*arr=(int*) malloc (3*sizeof(int)); arr[0]=1; arr[1]=3; arr[2]=2; printf ("%d",arr[arr[0]]); return 0; }
12th Sep 2020, 1:38 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
int* arr=(int*) malloc(3*sizeof(int)); //type errors removed. Check.. malloc is dynamic memory allocation method. Here allocating 3 locations of size int compatible of int* (Integer pointer type.) Then initializing values... arr[0]=1; arr[1]=3; arr[2]=2; printf("%d", arr[0]);
11th Sep 2020, 2:17 PM
Jayakrishna 🇮🇳
0
😅🐍🐍😆 thank you . But how it's output to come 3?
11th Sep 2020, 5:48 PM
Suparna Podder
Suparna Podder - avatar
0
Suparna Podder output comes 1 only for arr[0] If you getting 3,then I guessing you are given full statement as it giving error. So I guessing, you may missing there like arr[arr[0]]. This return 3 as output.. Inner arr[0] return 1, then outer arr[1] return 3.
11th Sep 2020, 7:06 PM
Jayakrishna 🇮🇳