Fork me on GitHub

Project Notes

#095 in C

All about pointers in C.

Notes

Pointers are arguably what makes C so versatile, and also the reason why C has proven to be such an unsafe language in practice.

Pointers are defined in the C language spec ISO/IEC 9899:TC3. Some interesting bits:

  • 6.2.5 Types
  • 6.7.5.1 Pointer declarators

Demo

demo.c exercises various pointer operations. The make file will compile and run:

$ make
gcc -Wall -O0    demo.c   -o demo
./demo

===== test_simple_pointers
Set void pointer to the address of an int: `int *p = &i`
Dereferencing int pointer: `*p`. Returns 42

===== test_void_pointers
Set void pointer to the address of an int: `void *p = &i`. Address: 0x7ffee92dd5ac
Casting a void pointer to assumed type and dereferencing: `*(int*)p`. Returns 42
Set int pointer equal to void pointer: `int *int_p = p`
Dereferencing int pointer: `*int_p`. Returns 42
But would not stop you setting float pointer equal to void pointer that points to int: `float *float_p = p`
Dereferencing the float pointer doesn't blow up (since float and int both 4 bytes), but value is wrong: 0.000000

===== test_arrays_and_pointers
Initializing a static array of int: `int i[2] = {42, 43}`
Set int pointer equal to the array variable (implicitly a pointer): `int *p = i`. Address: 0x7ffee92dd5a0
Dereferencing int pointer by index: `p[0]`, `p[1]`. Returns 42, 43
Dereferencing int pointer: `*p`, `*(p + 1)`. Returns 42, 43
Dereferencing array variable as a pointer: `*i`, `*(i + 1)`. Returns 42, 43

===== test_pointer_arrays
Initializing an array of int with 3 elements: {10, 20, 30}`
Setting pointers to the addresses of array elements (but reversed order): `p[i] = &values[size - 1 - i]`
Dereferencing elements from array of int pointers: `*p[i]`. Returns {30, 20, 10}`
Double dereferencing elements from array of int pointers: `**p`, `**(p + 1)`, `**(p + 2)`. Returns {30, 20, 10}`

===== test_list_of_strings
Initializing an array of char pointer strings: `char *names[] = {"A Name",...}`
Dereferencing char pointer: `*names[0]`, Returns A

Credits and References

About LCK#95 C

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

Project Source on GitHub Return to the LittleCodingKata Catalog
About LittleCodingKata

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

Follow the Blog follow projects and notes as they are published in your favourite feed reader