Basic of Pointer in C programming

The hard part of C programming

0

What is Pointer?

Pointer is simply a variable, but the value is memory address. Normally, a variable will have specific value but the pointer will have memory address of a value.

Before using pointer , we must first need to define it. It is same as defining a variable but only different is putting a star sign * before the variable name:

 

Usage of a Pointer

We need address operator to work with Pointer. And The address operator is &. This address operator will return the address of memory of a value. I have an example to show you that what a pointer hold :

In my computer the output is:

  • We first declare a variable val1 that is type of integer.
  • then declared a pointer *ptr and the value set to &val1. Mean address val1. Which is equal to 0x7ffdde45097c. And 0x7ffdde45097c Equal to 101.
  • Visually, the *ptr is 101.

So the pointer actually working indirectly to a variable…. right?

If i want to modify the value of the referenced variable, then i just need to set new value to *ptr pointer. Like this:

 

Another thing i tried to do is calling a function by reference. It was hard to understand , You guys are welcome to post comments if i have any wrong explanation. Here is the sample code:

 

  • I first declared the pointer when defined the function in line 3.
  • Declared a variable val1. And called the function with referenced to the variable val1.
  • val1 has 2. The value given to callbyref() function. *ptr2 taking the value doing a addition and responding the caller and modifying the main() function variable val1 value. Can you do it without pointer? How fun 🙂 .

 

This is all i currently done as basic of pointer. Any advice ? Suggestions? Please post comments below so that i can improve!