WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C · Basic · question 9 of 100

What are pointers, and how are they used in C?

📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

In C programming language, a pointer is a variable that stores the memory address of another variable. Pointers are used to indirectly access and modify the value of a variable, and they are essential for advanced memory manipulation in C. Here’s how pointers are defined and used in C:

Defining a pointer: To define a pointer in C, you need to declare the variable with the * operator before the variable name.

    int *ptr;

In this example, a pointer named ptr is defined that can store the memory address of an integer variable.

Assigning a value to a pointer: To assign a value to a pointer in C, you need to use the address-of operator & to get the memory address of the variable you want to store in the pointer.

    int value = 5;
    int *ptr = &value;

In this example, the value of value is assigned to ptr using the address-of operator &.

Dereferencing a pointer: To dereference a pointer in C, you need to use the * operator to access the value stored at the memory address that the pointer points to.

    int value = 5;
    int *ptr = &value;
    int dereferencedValue = *ptr;

In this example, the value of value is assigned to ptr using the address-of operator &. The value stored at the memory address pointed to by ptr is accessed using the dereferencing operator *, and the value is assigned to the variable dereferencedValue.

Here is an example that demonstrates the use of pointers in C:

    #include <stdio.h>
    
    int main() {
        int value = 5;
        int *ptr = &value;
        
        printf("The value of value is %dn", value);
        printf("The value of *ptr is %dn", *ptr);
        printf("The address of value is %pn", &value);
        printf("The value of ptr is %pn", ptr);
        printf("The value of &ptr is %pn", &ptr);
        
        *ptr = 10;
        printf("The value of value is now %dn", value);
        
        return 0;
    }

In this example, a pointer named ptr is defined and assigned the memory address of the variable value. The program prints the values of value, *ptr, the address of value, ptr, and &ptr to the console. The value of value is then changed by dereferencing ptr and assigning it a new value of 10.

Pointers are a powerful tool in C programming that allow developers to manipulate memory directly and perform complex operations on data structures. Understanding how pointers work and how to use them effectively is essential for writing efficient, effective C programs. However, improper use of pointers can lead to memory leaks, segmentation faults, and other runtime errors, so it is important to use pointers with care and caution.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C interview — then scores it.
📞 Practice C — free 15 min
📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

All 100 C questions · All topics