0% found this document useful (0 votes)
3 views

Pointers

A pointer is a variable that stores the memory address of another variable, allowing indirect access to its value. The document explains key concepts related to pointers, including dereferencing, pointer initialization, and their applications in functions and data structures. It also covers different types of pointers, such as null and generic pointers, and provides code examples illustrating their usage.

Uploaded by

nandithamr5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Pointers

A pointer is a variable that stores the memory address of another variable, allowing indirect access to its value. The document explains key concepts related to pointers, including dereferencing, pointer initialization, and their applications in functions and data structures. It also covers different types of pointers, such as null and generic pointers, and provides code examples illustrating their usage.

Uploaded by

nandithamr5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

What is a Pointer?

A pointer is like a map that shows the location of a treasure (the value of a variable). Instead of
carrying the treasure around, you just carry the map (pointer) that shows where to find it.

Key Terms:

 Variable: A place to store data.


 Pointer: A variable that stores the memory address of another variable.
 Dereferencing: When we use *ptr, we are dereferencing the pointer, meaning we are
using the address stored in the pointer to get the value stored there.

#include <stdio.h>

int main() {
int num = 10; // We create a variable 'num' and give it the value
10.
int *ptr; // We create a pointer 'ptr', which will store the
address of a variable.

ptr = &num; // We set 'ptr' to point to the address of 'num'. Now,


'ptr' knows where 'num' is stored.

// Printing the values


printf("Value of num: %d\n", num); // This prints the value stored
in 'num', which is 10.
printf("Address of num: %p\n", &num); // This prints the memory
address where 'num' is stored.
printf("Value stored at ptr: %d\n", *ptr); // This prints the value that
'ptr' is pointing to, which is 10.
printf("Address stored in ptr: %p\n", ptr); // This prints the address
'ptr' is pointing to (address of 'num').

return 0;
}

Ste Memory
Code Explanation
p (Visualized)

A variable num is created and assigned the


1 int num = 10; num → 10
value 10.

2 int *ptr; A pointer ptr is declared. It is meant to store ptr →


the address of another variable. (uninitialized)
Ste Memory
Code Explanation
p (Visualized)

The pointer ptr is assigned the address of


ptr → Address
3 ptr = &num; num. Now ptr points to the location where
of num
num is stored.

printf("Value of num:
4 The value of num is printed, which is 10. Output: 10
%d\n", num);

printf("Address of The memory address where num is stored is Output: Address


5
num: %p\n", &num); printed. of num

The value stored at the address ptr is


printf("Value stored at
6 pointing to is printed, which is 10 (since ptr Output: 10
ptr: %d\n", *ptr);
points to num).

printf("Address stored The address stored in ptr is printed. This Output: Address
7
in ptr: %p\n", ptr); address is where num is stored. of num

Memory Visualization:
 Memory after Step 1:
num is created with the value 10.
 num: 10
 Memory after Step 2:
ptr is created, but it's not initialized yet.
 num: 10
 ptr: (uninitialized)
 Memory after Step 3:
ptr is now set to point to the address of num.
 num: 10
 ptr: Address of num (e.g., 0x7ffee9b08b7c)
 Output:
o Value of num = 10

o Address of num = Address of num (e.g., 0x7ffee9b08b7c)

o Value stored at ptr = 10 (since ptr points to num)

o Address stored in ptr = Address of num (same as above)


Applications of Pointers
1. Pass information between functions.
2. Return multiple data items from a function.
3. Access elements of an array alternatively.
4. Pass arrays and strings as function arguments.
5. Create complex data structures (e.g., trees, linked lists).
6. Allocate memory dynamically.

Declaring Pointer Variables


 Syntax: data_type *pointer_name;
o data_type: Type of value the pointer will point to (e.g., int, float).

o *: Indicates it is a pointer.

o pointer_name: Name of the pointer variable.

Examples:
int *ptr; // Pointer to an integer
float *temp; // Pointer to a float

Operators Used with Pointers


1. Address-of operator (&): Gives the address of a variable.
2. Indirection operator (*): Accesses the value stored at the pointer’s
address.
Example:
int a = 3;
int *ptr;
ptr = &a;
printf("Address: %d, Value: %d\n", ptr, *ptr);

Types of Pointers
1. Null Pointer:
o Does not point to any valid memory location.
o Declared as: int *ptr = NULL;

o Useful when a pointer needs to point to different locations at different


times.
#include <stdio.h>
int main() {
int *ptr = NULL; // Declare a null pointer
// Check if the pointer is null
if (ptr == NULL) {
printf("The pointer is null and does not point to any memory location.\n");
} else {
printf("The pointer is not null.\n");
}
return 0;
}
Output: The pointer is null and does not point to any memory location.

2. Generic Pointer:
o A pointer with void as its data type, meaning it can point to any data
type.
o Declared as: void *ptr;

o Must be typecast before use.(Type Cast- Type casting in programming


means changing one data type into another. It is a way to tell the
computer to treat a variable as if it is of a different type for a specific
purpose.)

#include <stdio.h>
int main() {
void *ptr; // Declare a void pointer

int num = 42; // Declare an integer variable


float fnum = 3.14; // Declare a float variable

// Assign the address of the integer to the void pointer


ptr = &num;
printf("Value of integer: %d\n", *(int *)ptr); // Cast to int* and dereference

// Assign the address of the float to the void pointer


ptr = &fnum;
printf("Value of float: %.2f\n", *(float *)ptr); // Cast to float* and
dereference

return 0;
}
Explanation:
1. void *ptr;
o A void pointer is declared. It can point to any data type but cannot be
directly dereferenced.
2. int num = 42;
o An integer variable num is declared and initialized with the value 42.

3. ptr = &num;
o The address of num is assigned to the void pointer ptr.

4. *(int *)ptr
o The void pointer is cast to an integer pointer (int *) and then
dereferenced to access the value.
5. ptr = &fnum;
o The address of the float variable fnum is assigned to the void pointer.

6. *(float *)ptr
o The void pointer is cast to a float pointer (float *) and then
dereferenced to access the value.

Output:
Value of integer: 42
Value of float: 3.14

Pointer Initialization
 Assign the address of a variable to a pointer during or after declaration.
 Syntax:
int a;
int *ptr = &a; // or
ptr = &a;

Passing Pointers to Functions


 Passing pointers allows modifying the original variable from a different
function.
 Steps:
1. Declare function parameters as pointers.
2. Use dereferencing inside the function to modify the value.
3. Pass the address of variables as arguments.
Example: Adding Two Numbers
#include<stdio.h>
int add(int *a, int *b) {
return *a + *b;
}
void main() {
int a, b, res;
printf("Enter values of a and b: ");
scanf("%d %d", &a, &b);
res = add(&a, &b);
printf("Result = %d\n", res);
}
Output:
Enter values of a and b: 4 5
Result = 9

Swapping Two Numbers Using Pointers


Example:
#include<stdio.h>
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void main() {
int a, b;
printf("Enter values of a and b: ");
scanf("%d %d", &a, &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swapping: a = %d, b = %d\n", a, b);
}
Output:
Enter values of a and b: 10 20
Before swapping: a = 10, b = 20
After swapping: a = 20, b = 10
 #include<stdio.h>
 Includes the library needed for input (scanf) and output (printf) functions.
 void swap(int *a, int *b)
 This function is used to swap two numbers.
 It takes the memory addresses of the variables a and b as input parameters.
 Inside the swap function:
 int temp;
o A temporary variable temp is created to hold one value during the
swap process.
 temp = *a;
o The value of a (pointed to by *a) is stored in temp.

 *a = *b;
o The value of b (pointed to by *b) is assigned to a.

 *b = temp;
o The value stored in temp is assigned to b.

 void main()
 This is the main function where the program starts execution.
 Two integers a and b are declared to store user input.
 printf and scanf:
 printf("Enter values of a and b: ");
o Prompts the user to enter two numbers.

 scanf("%d %d", &a, &b);


o Reads the input values and stores them in a and b.

 Before Swap:
 printf("Before swapping: a = %d, b = %d\n", a, b);
o Prints the values of a and b before the swap.

 Call to swap:
 swap(&a, &b);
o The memory addresses of a and b are passed to the swap function.

o Inside the swap function:


 The values of a and b are exchanged using the temporary
variable.
 After Swap:
 printf("After swapping: a = %d, b = %d\n", a, b);
o Prints the values of a and b after the swap.

You might also like