Pointer in C Programming
Pointer
A pointer is a special variable that stores the memory address of another variable.
Unlike a normal variable that stores a value directly, a pointer stores the address where another variable is located in memory.
A pointer must also be declared before it can be used in a C program.
Example
Suppose we declare an integer variable:
int a;
To store the address of a, we need an integer pointer:
int *p;
Here:
ais an integer variable.pis an integer pointer variable.pcan store the memory address of an integer variable such asa.
For example:
p = &a;
The pointer should generally be declared with a type compatible with the variable whose address it stores.
For example:
int *p;
float *f;
char *c;
Address Operator (&)
The address operator (&) is a unary operator used to obtain the memory address of a variable.
Syntax
&variable_name
Example
int a = 10;
printf("%p", (void *)&a);
Here:
&a
represents the memory address of variable a.
Declaration of Pointer Variable
A pointer variable is declared using the * symbol.
Syntax
data_type *pointer_variable;
Integer Pointer
int *p;
Here, p is an integer pointer that can store the address of an integer variable.
Character Pointer
char *c;
Here, c is a character pointer that can store the address of a character variable.
Float Pointer
float *d;
Here, d is a float pointer that can store the address of a floating-point variable.
Pointer Initialization
Pointer initialization means assigning the address of a variable to a pointer.
Syntax
pointer_variable = &normal_variable;
Example
int a = 57;
int *p;
p = &a;
Here:
acontains the value57.&arepresents the address ofa.pstores the address ofa.
Therefore:
p = &a;
means that pointer p points to variable a.
Referencing a Pointer
Referencing refers to making a pointer point to another variable by assigning the address of that variable to the pointer.
Example
int a = 57;
int *p;
p = &a;
Here:
&a
gives the address of a, and:
p = &a;
stores that address inside pointer p.
Dereferencing a Pointer
Dereferencing is the process of accessing the value stored at the memory address held by a pointer.
The * operator is used for dereferencing.
Syntax
*pointer_variable
Example
int a = 20;
int *p;
p = &a;
Here:
*p
returns the value stored in a.
Therefore:
a = 20
*p = 20
Difference Between & and *
| Operator | Meaning | Example |
|---|---|---|
& | Returns the address of a variable | &a |
* | Returns the value stored at the address held by a pointer | *p |
Example Program of Pointer
#include <stdio.h>
int main(void)
{
int a = 20;
int *p;
p = &a;
printf("Address of a: %p\n", (void *)&a);
printf("Value stored in p: %p\n", (void *)p);
printf("Value of a: %d\n", a);
printf("Value pointed to by p: %d\n", *p);
return 0;
}
Explanation
Consider:
int a = 20;
Variable a contains:
20
Now:
int *p;
declares an integer pointer.
When we write:
p = &a;
the address of a is stored in p.
Therefore:
a → Value of a
&a → Address of a
p → Address stored in pointer p
*p → Value stored at the address pointed to by p
Since p points to a:
*p = a
and both give:
20
Simple Pointer Representation
Variable a
┌─────────────┐
│ 20 │
└─────────────┘
↑
│
│ points to
│
Pointer p
┌─────────────┐
│ Address of a│
└─────────────┘
So:
p = &a;
and:
*p
gives the value of a.
Quick Revision
Pointer
A pointer is a special variable that stores the memory address of another variable.
Example
int *p;
Address Operator
The & operator gives the address of a variable.
&a
Pointer Initialization
int a = 57;
int *p;
p = &a;
Dereferencing
The * operator is used to access the value stored at the address held by a pointer.
*p
Important Example
int a = 20;
int *p;
p = &a;
Then:
a = 20
*p = 20
while both:
&a
p
represent the address of a.
Important Exam Points
- A pointer stores the memory address of another variable.
- A pointer is declared using the
*symbol. int *p;declares an integer pointer.char *p;declares a character pointer.float *p;declares a floating-point pointer.- The
&operator returns the address of a variable. p = &a;makes pointerppoint to variablea.- The
*operator is used to dereference a pointer. - If
ppoints toa, then*pgives the value stored ina. pstores an address, while*paccesses the value at that address.
Advantages of Pointer
The provided source introduces “Advantages of Pointer” but does not include the individual advantage points.
Discussion
Share a helpful question, idea, or explanation with other students.