Skip to content

Instantly share code, notes, and snippets.

@euske
Created June 19, 2020 03:27
Show Gist options
  • Select an option

  • Save euske/6e714feb3e0c5c1ebceb3094c11cbae3 to your computer and use it in GitHub Desktop.

Select an option

Save euske/6e714feb3e0c5c1ebceb3094c11cbae3 to your computer and use it in GitHub Desktop.
C Pointer Exercises for Beginners
/*
C Pointer Exercises for Beginners
by Yusuke Shinyama
*/
#include <stdio.h>
int main(int argc, char **argv)
{
struct foo {
int x;
int *y;
int z[3];
};
int a[] = { 1, 2, 3 };
int b = 4;
struct foo c = {0};
c.x = 5;
c.z[0] = 7;
c.z[1] = 8;
c.z[2] = 9;
int *p1 = &b;
int **p2 = &p1;
struct foo *p3 = &c;
int p4 = c.x;
int *p5 = &p4;
int *p6 = &(c.x);
int *p7 = &(p3->x);
int p8 = a[1];
int *p9 = &(a[1]);
int *p10 = a+1;
int *p11 = c.y;
int **p12 = &(c.y);
int **p13 = &(p3->y);
int *p14 = p3->z;
int *p15 = &(p3->z[0]);
int p16 = p3->z[1];
void *p17 = &(c.z[1]);
int *p18 = c.z+1;
int *p19 = p9+1;
/* Question.
Without running the program,
answer the following for each expression:
a. the exact value if it is known, or
b. the expression(s) that has the same value (if any).
*/
printf("p1=%p\n", p1);
printf("*p1=%p\n", *p1);
printf("p2=%p\n", p2);
printf("*p2=%p\n", *p2);
printf("**p2=%p\n", **p2);
printf("p3=%p\n", p3);
printf("p4=%p\n", p4);
printf("p5=%p\n", p5);
printf("*p5=%p\n", *p5);
printf("p6=%p\n", p6);
printf("*p6=%p\n", *p6);
printf("p7=%p\n", p7);
printf("*p7=%p\n", *p7);
printf("p8=%p\n", p8);
printf("p9=%p\n", p9);
printf("*p9=%p\n", *p9);
printf("p10=%p\n", p10);
printf("*p10=%p\n", *p10);
printf("p11=%p\n", p11);
printf("p12=%p\n", p12);
printf("*p12=%p\n", *p12);
printf("p13=%p\n", p13);
printf("*p13=%p\n", *p13);
printf("p14=%p\n", p14);
printf("*p14=%p\n", *p14);
printf("p15=%p\n", p15);
printf("*p15=%p\n", *p15);
printf("p16=%p\n", p16);
printf("p17=%p\n", p17);
printf("p18=%p\n", p18);
printf("*p18=%p\n", *p18);
printf("p19=%p\n", p19);
printf("*p19=%p\n", *p19);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment