+ 2

what is teh output of the snippet code qnd how??

#include <stdio.h> int main() { static int a[]={10,20,30,40,50}; static int *p[]={a,a+3,a+4,a+1,a+2}; int **ptr=p; ptr++; printf("%d%d",ptr-p,**ptr); return 0; }

5th Jan 2022, 12:56 PM
Nariman Tajari
Nariman Tajari - avatar
9 odpowiedzi
+ 2
#include <stdio.h> int main() { static int a[]={10,20,30,40,50}; printf("%d %d %d\n", *a, a[0], a[3]); // 10 10 40 printf("%p %p %p\n", a, &a[0], &a[3]); // 0x404040 0x404040 0x40404c printf("%ld\n", sizeof(int)); // 4 static int *p[]={a,a+3,a+4,a+1,a+2}; printf("%p %p %p\n", *p, p[0], p[1]); // 0x404040 0x404040 0x40404c printf("%p %p %p\n", p, &p[0], &p[1]); // 0x404060 0x404060 0x404068 printf("%ld\n", sizeof(int*)); // 8 int **ptr=p; printf("%p %p\n", ptr, p); // 0x404060 0x404060 ptr++; // 0x404060 add 1 * sizeof(int*) printf("%p %p\n", ptr, p); // 0x404068 0x404060 printf("%d %ld\n", 0x404068 - 0x404060, ptr-p); //8 1, I don't know the reason, subtract two array pointer will get the number of elements between them.(address1 - address2)/sizeof(element) printf("%p %p %d\n", ptr, *ptr, **ptr); // 0x404068 0x40404c 40 printf("%ld %d",ptr-p,**ptr); //1 40 return 0; }
5th Jan 2022, 4:17 PM
FanYu
FanYu - avatar
+ 1
Martin Taylor i always run the codes before asking but the procedure of pointers here was vague forme, i know the output but not the procedure and debugging.
5th Jan 2022, 2:05 PM
Nariman Tajari
Nariman Tajari - avatar
+ 1
The tip is very explicit. printf("%ld %d", ptr-p, **p); ./Playground/file0.c: In function 'main': ./Playground/file0.c:15:10: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=] 15 | printf("%d %d", ptr-p, **p); | ~^ ~~~~~ | | | | int long int | %ld
5th Jan 2022, 2:27 PM
FanYu
FanYu - avatar
+ 1
output is my question not the sololearn's compiler error. i donno why the output is 140 𝓕𝓛𝓨
5th Jan 2022, 2:31 PM
Nariman Tajari
Nariman Tajari - avatar
0
is it the Clion debugging 𝓕𝓛𝓨 ? its nice but no comprehensible for mi sir
5th Jan 2022, 4:51 PM
Nariman Tajari
Nariman Tajari - avatar
0
Nariman Tajari I don't understand the word 'Clion'. we know & means get the address * means get the value of the address 0x40404c is the address of 40 0x404068 is the address of 0x404068 (40's address)
5th Jan 2022, 5:05 PM
FanYu
FanYu - avatar