HOME HTML EDITOR C JAVA PHP

C Pointers: The Power of Indirect Access

A **Pointer** is a variable that stores the memory address of another variable as its value. Instead of holding a piece of data (like the number 5), it "points" to the location where that data is stored. This allows you to manipulate data directly in memory, which is much faster and more flexible than working with copies of variables.

1. The Pointer Syntax

To work with pointers, you need to understand two special operators:

Declaration and Initialization:

int myAge = 25; // An int variable
int* ptr = &myAge; // A pointer variable that stores the address of myAge

// Output the value of myAge (25)
printf("%d\n", myAge);

// Output the memory address of myAge (e.g., 0x7ffe...)
printf("%p\n", &myAge);

// Output the memory address stored in ptr (Same as &myAge)
printf("%p\n", ptr);

2. Dereferencing: Getting the Value

Once you have a pointer storing an address, you can use the * operator again to "dereference" it. This tells the computer: "Go to this address and give me the value stored there."

int myNum = 100;
int* ptr = &myNum;

// Dereferencing ptr to get the value
printf("Value: %d", *ptr); // Output: 100

3. Why Use Pointers?

Pointers are not just for showing off; they are necessary for professional C development:

4. Pointers and Arrays

In C, the name of an array is actually a pointer to its first element. This is why you don't need the & symbol when reading a string with scanf().

int myArr[3] = {10, 20, 30};
printf("Address of first element: %p\n", myArr);
printf("Value of first element: %d\n", *myArr); // Output: 10

5. The NULL Pointer

A pointer that is not assigned any address is called an uninitialized pointer. This is dangerous. It is best practice to assign a NULL value to a pointer if you don't have an address for it yet. A NULL pointer points to nothing.

int* ptr = NULL;

6. Comparison of Reference vs. Dereference

Operator Name Action
& Address-of (Reference) Returns the memory address of a variable.
* Value-at-address (Dereference) Returns the value stored at the address.

7. Common Pitfalls

Pro Tip: Think of a pointer as a "Shortcut" on your computer desktop. The shortcut is not the file itself; it’s just a small link that knows exactly where the big file is stored on the hard drive.