Pointers for beginners

Dalhat Muhammad
3 min readMar 16, 2023

Hello readers! I am excited to write my first technical article on pointers in C programming. Pointers are a crucial feature of low-level languages such as C and C++, and they can be difficult to understand at first. However, mastering pointers is key to becoming a great programmer. In this article, I will provide a beginner-friendly explanation of pointers and how to use them in C programming.

What are Pointers?

Pointers are variables that hold the memory address of another variable. This means that a pointer points to the memory address of a variable, not the variable itself. To simplify, if a variable is a building, then a pointer is the address of the building, such as "221b Baker Street," for example. Pointers act as virtual directions to data, allowing us to interact with it remotely, much like being able to access a house without physically traveling there, resulting in more efficient data manipulation.

Declaring Pointers:

To declare a pointer in C, we use the asterisk symbol (*) before the pointer variable name. Here is an example of declaring an integer pointer:

int *ptr; 

This declaration indicates that we are declaring a pointer to an integer variable. The asterisk symbol before the variable name tells the compiler that this is a pointer variable.

Assigning Pointers:

After declaring a pointer, we must assign it to a variable before we can use it. We do this by using the address-of operator (&) before the variable name. Here is an example of assigning a pointer to an integer variable:

int num = 10; int *ptr = # 

This code assigns the memory address of the variable "num" to the pointer "ptr." The "&" operator returns the memory address of the variable "num," and the pointer "ptr" points to that memory address. In C, a memory address refers to a unique identifier assigned to a location in the computer's memory. Every variable, pointer, or function in a program is stored in a specific memory location, which can be accessed using its memory address. Memory addresses are represented as hexadecimal numbers. However, care must be taken to avoid memory-related errors, such as accessing invalid memory addresses or writing to read-only memory.

Dereferencing Pointers:

Dereferencing a pointer means accessing the value of the variable it points to. We use the asterisk symbol (*) before the pointer variable name to dereference a pointer. Here is an example:

int num = 10; int *ptr = # printf("The value of num is: %d\n", *ptr);

This code prints the value of the variable "num" by dereferencing the pointer "ptr." The "*" operator before the pointer variable name tells the compiler to access the value of the variable it points to.

Pointer Arithmetic:

Pointer arithmetic allows us to perform arithmetic operations on pointer variables. This is useful when we want to access multiple elements in an array, for example. Here is an example of using pointer arithmetic:

int arr[] = {1, 2, 3, 4, 5}; int *ptr = arr; printf("The value of the second element in the array is: %d\n", *(ptr+1)); 

This code assigns the memory address of the first element of the array "arr" to the pointer "ptr." We can use pointer arithmetic to access the second element in the array by adding 1 to the memory address of the first element. The "*" operator before the pointer variable name tells the compiler to access the value of the variable it points to.

In conclusion, pointers are a powerful feature of the C programming language that allow us to manipulate data in memory efficiently. They enable us to access and modify data remotely

--

--

Dalhat Muhammad
0 Followers

Learning software engineering and writing about the things I learn