Pointers in C
Pointer Variable in C
A pointer variable is a variable that stores the memory address of another variable.
They are called pointers because they point to a specific location in memory.
Memory Allocation of Variables
When a variable is declared:
-
It is stored in a specific memory location.
-
The compiler and operating system automatically decide where it is placed at runtime.
-
The programmer does not control the exact memory location.
Example Declaration
This tells the C compiler to:
-
Reserve space in memory to store an integer value
(location/address decided by compiler/OS) -
Associate the name
xwith that memory location -
Store the value
3in that location
Accessing Variable Information
1. Size of Allocated Space
2. Value Stored in Variable
3. Memory Address of Variable
Use the address-of operator &
-
&xgives the address of variablex
Declaring a Pointer Variable
Pointers are declared using the indirection operator *
Example
-
Creates a pointer variable
p -
It can store the address of an integer variable
Assigning Address to Pointer
-
Now
pcontains the address ofx -
We say
ppoints tox
Understanding a Pointer
A pointer has two parts:
-
The pointer itself → holds the address
-
The address → points to the stored value
Printing Pointer Information
1. Print Address Stored in Pointer
2. Print Value Pointed by Pointer
-
*pmeans value stored at the address contained inp
Uses of Pointers in C
Pointers can be used to:
-
Build faster and more efficient code
-
Provide an alternate and efficient way to access data stored in variables and arrays
-
Pass arrays and strings conveniently from one function to another
-
Return more than one value from a function
-
Perform dynamic memory allocation
-
Build complex data structures such as linked lists, trees, etc.
Important Pointer Operators in C
1. Address-of Operator (&)
-
Used to get the memory address of a variable
-
Returns the location where the variable is stored in memory
Example:
Returns the address of variable
2. Dereference Operator (*)
-
Used to access the value stored at a pointer’s address
-
Also used to declare pointer variables
Example:
Accesses the value stored at the memory location pointed to by pointer
Types of Pointers in C
1. NULL Pointer
-
A pointer that does not point to any valid memory location
-
Created by assigning NULL to the pointer
-
Any pointer type can be assigned NULL
Syntax:
2. Void Pointer (Generic Pointer)
-
Pointer of type void
-
Has no associated data type
-
Can store address of any type of variable
Syntax:
3. Constant Pointer
-
The address stored in the pointer is constant
-
Cannot be modified after initialization
-
Always points to the same memory location
Syntax:
4. Pointer to Constant
-
Pointer that points to a constant value
-
The value cannot be modified through the pointer
Syntax:
5. Dangling Pointer
-
A pointer that points to a memory location that has been freed or deleted
-
Occurs when memory is released but the pointer still stores the old address
int main()
{
int num=5;
int *ptr=#
printf(“the value and address of num is %d %p\n”,num,&num);
printf(“the value and address of num using pointer ptr is %d %p\n”,*ptr,ptr);
*ptr=15; // indirection, which stores value in variable num using the pointer ptr.
printf(“the value of num after indirect initialization is %d\n”,num);
}
void f(int *x) // x is a pointer to n
{
*x=*x+1; //accessing the actual content
}
int main()
{
int n=10;
f(&n); //calling the function with address/reference
printf("n in main =%d\n",n);
//note..... value of n changed in main
}

Comments
Post a Comment