CTSD Pract3 - Merged
CTSD Pract3 - Merged
: 03
Title:
1. Write a program to demonstrate how to access a variable using its pointer.
2. Write a program to swap two numbers using pointers.
3. Write a program to demonstrate accessing array element using pointers.
Introduction to Pointers
A pointer is a variable that stores the memory address of another variable. Pointers are widely
used in C programming to efficiently manipulate data, allocate memory dynamically, and
optimize performance.
Concept of Pointers
A pointer variable is declared using the * (asterisk) symbol. It holds the address of another
variable, allowing indirect access to that variable’s value. The & (address-of) operator is used
to retrieve the memory address of a variable.
Syntax of Pointers
data_type *pointer_name;
Here, data_type represents the type of data the pointer will point to, and pointer_name is the
name of the pointer variable.
3. Access the value of the variable using the pointer and the * (dereference) operator.
Introduction
Swapping two numbers means exchanging their values. This can be done using different
methods such as using a temporary variable, arithmetic operations, or bitwise XOR. In this
case, we will swap two numbers using pointers, which allows direct manipulation of memory
addresses.
The name of an array acts as a pointer to its first element. That means, if arr is an array, then:
Using pointers, we can iterate through the array without using the traditional subscript
notation ([]).
Conclusion :
int main()
{
int num = 10; // Declare an integer variable
int *ptr = # // Declare a pointer and store the address of num
return 0;
}
Output:-
3.2
#include <stdio.h>
int main()
{
int n1, n2; // Declare two integer variables
// Ask the user to enter two numbers
printf("Enter the values of n1 and n2:\n");
scanf("%d %d", &n1, &n2); // Read input values from the user
Output:-
3.3
// Write a program to demonstrate accessing array elements using pointers
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5}; // Declare and initialize an array
int i, *ptr = arr; // Declare a pointer and assign it to the first element of the array
printf("Accessing array elements using pointers:\n");
// Loop through the array using a pointer
for (i = 0; i < 5; i++)
{
printf("Element %d: %d\n", i, *(ptr + i)); // Access array elements using pointer
arithmetic
}
return 0; // End of program
}
Output:-
In C programming, an array of pointers to strings is an efficient way to store and manipulate multiple
strings. Instead of using a 2D character array, we store string literals as pointers, making memory
usage more efficient.
Pointer to a Pointer in C
Introduction
In C, a pointer to a pointer (also called a double pointer) is a pointer that stores the address of another
pointer. It allows multiple levels of indirection, meaning that instead of pointing to a variable, it points
to another pointer that in turn points to a variable.
1. Call by Value – A copy of the variable is passed to the function. Any changes inside the
function do not affect the original value.
2. Call by Reference – A memory address (pointer) of the variable is passed to the function. Any
changes inside the function affect the original value.
Call by Value
● In Call by Value, the function creates a local copy of the passed variable.
Call by Reference
● In Call by Reference, a pointer to the original variable is passed to the function.
● The function modifies the actual value stored at the memory address.
Use Cases When we don't want the function to When we need to modify the
modify the original value original variable
Conclusion:-
int main()
{
// Declare an array of string pointers (array of character pointers)
// Each element is a pointer to a string literal
const char *strings[] = {"Hello", "Welcome", "to", "Parul", "University"};
// Get the number of elements in the array (size of array / size of one element)
int i, size = sizeof(strings) / sizeof(strings[0]);
// Print a message indicating that strings will be accessed using an array of pointers
printf("Accessing strings using an array of pointers:\n");
Output :-
4.2
#include <stdio.h> // Include standard input-output library for printf function
int main()
{
int num = 10; // Declare an integer variable and initialize it with 10
int *ptr = # // Declare a pointer `ptr` and store the address of `num`
int **ptr2 = &ptr; // Declare a pointer to a pointer `ptr2` and store the address of `ptr`
Output :-
4.3
#include <stdio.h> // Include standard input-output library for printf function
int main()
{
Output :-