+ 1

Can anyone please explain the working of below code snippet how pointer is pointing and printing the values.

#include<stdio.h> struct list { int a; char *ch; } s[] = {18, "GATE", 19, "CSE", 20, "COURSE"}; int main() { struct list *ptr = s; printf("%s\n", ptr++->ch); printf("%s\n", (++ptr)->ch); printf("%d\n", (--ptr)[1].a); printf("%d\n", ptr[0].a); return 0; }

8th Aug 2021, 5:55 AM
Sanaya Singhaniya
Sanaya Singhaniya - avatar
1 Antwort
+ 1
I tried my best explaining with comments in the code bit. But first you must know the following shorthands: ptr->ch; is the same as: *(ptr).ch (ptr++)->ch; is the same as: *(ptr).ch; ptr++; ptr[1]; is the same as: *(ptr+1).a; https://code.sololearn.com/cfG45zKw4GRP/?ref=app
8th Aug 2021, 6:52 AM
Giorgos