Struct Pointer Arrow Operator in C
Level 5253 keystrokes
Creates a Point struct and a pointer to it, reads the members with the dot operator on the struct and with the arrow operator on the pointer, then changes them through the pointer. It prints (10, 20) twice and then (30, 40), because the original struct was modified.
C code
// A point with two integer coordinates1struct Point { // Horizontal coordinate2 int x; // Vertical coordinate3 int y;4};5 // A Point with x = 10 and y = 206struct Point p1 = {10, 20};// ptr holds the address of p17struct Point *ptr = &p1;8 // The dot operator works on the struct itself: prints (10, 20)9printf("Using dot: (%d, %d)\n", p1.x, p1.y);// The arrow works on a pointer; ptr->x means (*ptr).x, so this also prints (10, 20)10printf("Using arrow: (%d, %d)\n", ptr->x, ptr->y);11 // Writes new coordinates through the pointer12ptr->x = 30;13ptr->y = 40;// ptr points to p1, so p1 itself changed: prints Modified: (30, 40)14printf("Modified: (%d, %d)\n", p1.x, p1.y);Output
Using dot: (10, 20) Using arrow: (10, 20) Modified: (30, 40)
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this C code and how often they appear.
,× 10;× 10"× 6(× 6)× 6%× 6-× 4.× 4=× 4>× 4