Pointers in C

By | February 15, 2023

Pointers are a fundamental concept in the C programming language, which allow direct access to memory locations. A pointer is a variable that holds a memory address, which points to the location of a value in memory. Pointers are used to pass information between functions, dynamically allocate memory, and access data structures.

In C, pointers are declared using the asterisk (*) symbol. Here’s an example of how to declare a pointer:

int *ptr;

This declares a pointer variable named “ptr” that can store the memory address of an integer value.

Here are some basic operations that can be performed on pointers in C:

  1. Address of operator (&) –
    The address-of operator (&) is used to get the memory address of a variable. Here’s an example:

    int num = 42;
    int *ptr = #
    

    This sets the value of “ptr” to the memory address of “num”.

  2. Dereferencing operator (*) –
    The dereferencing operator (*) is used to access the value stored at a memory address. Here’s an example:

    int num = 42;
    int *ptr = #
    printf("%d\n", *ptr);
    

    This prints the value of “num” (42) by dereferencing the pointer “ptr”.

  3. Pointer arithmetic –
    Pointer arithmetic allows you to perform arithmetic operations on pointers, such as adding or subtracting an integer value to the pointer. Here’s an example:

    int arr[3] = {1, 2, 3};
    int *ptr = arr;
    printf("%d\n", *ptr);     // prints 1
    ptr++;
    printf("%d\n", *ptr);     // prints 2
    

    This sets the value of “ptr” to the memory address of the first element of the array “arr”. Then, the pointer is incremented by one, which points to the next element of the array.

  4. Dynamic memory allocation –
    Dynamic memory allocation allows you to allocate memory at runtime, rather than at compile time. This is done using the “malloc” function, which returns a pointer to the allocated memory. Here’s an example:

    int *ptr = (int*)malloc(10 * sizeof(int));
    

    This allocates 10 integers worth of memory and sets the value of “ptr” to the memory address of the first integer.

Note that when using pointers, it’s important to be careful and avoid common pitfalls like dereferencing uninitialized or null pointers, and accessing memory beyond its allocated bounds. These can lead to memory errors and crashes in your program.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.