Just a simple program to see the effects of pointer initialization in C.
Program:
#includevoid main() { int *p; printf("&p = %p, p = %p\n", &p, p); /* // This will cause segmentation fault *p = 5; printf("*p = %d\n", *p); */ int *q = 0; printf("&q = %p, q = %p\n", &q, q); // printing *q will cause segfault int *r; r = 0; printf("&r = %p, r = %p\n", &r, r); // printing *r will cause segfault int *s = NULL; printf("&s = %p, s = %p\n", &s, s); // printing *s will cause segfault int *t; t = NULL; printf("&t = %p, t = %p\n", &t, t); // printing *t will cause segfault int *u = (int *) 1; // point to address 0x01, not set value to 1 printf("&u = %p, u = %p\n", &u, u); // printing *u will cause segfault int *v = (void *) 2; // point to address 0x02, not set value to 2 printf("&v = %p, v = %p\n", &v, v); // printing *v will cause segfault int w = 5; int *x = &w; printf("&w = %p, w = %d\n", &w, w); // *w and w[0] will not compile printf("&x = %p, x = %p, *x = %d, x[0] = %d\n", &x, x, *x, x[0]); char *a = "A"; // using single quotes 'A' will not compile printf("&a = %p, a = %p, *a = %c, a[0] = %c\n", &a, a, *a, a[0]); // Cannot use %s format specifier - must use %c char *b = "BC"; // overflow by 1 char char *c = b + 1; // point to address 1 byte after b printf("&b = %p, b = %p, *b = %c, b[0] = %c, b[1] = %c\n", &b, b, *b, b[0], b[1]); printf("&c = %p, c = %p, *c = %c, c[0] = %c\n", &c, c, *c, c[0]); }
Sample Output:
&p = 0x7ffc2c5314a0, p = 0x5f5f00656d697474 &q = 0x7ffc2c5314a8, q = (nil) &r = 0x7ffc2c5314b0, r = (nil) &s = 0x7ffc2c5314b8, s = (nil) &t = 0x7ffc2c5314c0, t = (nil) &u = 0x7ffc2c5314c8, u = 0x1 &v = 0x7ffc2c5314d0, v = 0x2 &w = 0x7ffc2c53149c, w = 5 &x = 0x7ffc2c5314d8, x = 0x7ffc2c53149c, *x = 5, x[0] = 5 &a = 0x7ffc2c5314e0, a = 0x4008f5, *a = A, a[0] = A &b = 0x7ffc2c5314e8, b = 0x40091d, *b = B, b[0] = B, b[1] = C &c = 0x7ffc2c5314f0, c = 0x40091e, *c = C, c[0] = C