Chapter 4- Programming in C----> Pointer
Pointers in C are similar to as other variables that we use to hold the data in our program but, instead of containing actual data, they contain a pointer to the address (memory location) where the data or information can be found.
These are an important and advance concept of C language since, variables names are not sufficient to provide the requirement of user while developing a complex program. However, use of pointers help to access memory address of that entities globally to any number of functions that we use in our program.
Importance of Pointer.
While using several numbers of functions in C program, every functions should be called and value should be passed locally. However, using pointer variable, which can access the address or memory location and points whatever the address (memory location) contains.
Pointer declaration
Eg, int *age;
Advantages
1. It helps us to access a variable that is not defined within a function.
2. It helps to reduce program length and complexity i.e. faster program execution time.
3. It is more convenient to handle data.
4. It helps to return one or more than one value from the functions.
Program example:
#include <stdio.h>
int main()
{
int n, *ptr;
printf("Enter any number:");
scanf("%d", &n);
ptr = &n;
printf("Value entered: %d\n", n);
printf("Value through pointer: %d\n", *ptr);
return 0;
}
1. Write a program to swap two integers using pointers.
#include <stdio.h>
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x, y;
printf("Enter two integers: ");
scanf("%d %d", &x, &y);
printf("Before swapping: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
2.Write a program to reverse an integer using pointer manipulation.
#include <stdio.h>
void reverse(int *n, int *rev) {
int num = *n;
*rev = 0;
while (num != 0) {
int digit = num % 10;
*rev = (*rev * 10) + digit;
num /= 10;
}
}
int main() {
int number, reversed;
printf("Enter an integer: ");
scanf("%d", &number);
reverse(&number, &reversed);
printf("Original number: %d\n", number);
printf("Reversed number: %d\n", reversed);
return 0;
}
Define pointer. Write the advantages of pointers.
A
pointer in C is a variable that stores
the memory address of another variable.
Instead
of holding a data value directly, it "points" to the location in
memory where the value is stored.
Advantages
of Pointers
· Efficient memory access: Direct access to memory addresses.
·
Dynamic
memory allocation: Helps in creating flexible memory management using `malloc`,
`calloc`, `free`.
·
Array
and string handling: Simplifies operations on arrays and strings.
·
Function
arguments: Enables call by reference, so functions can modify actual values.
·
Data
structures: Essential for linked lists, trees, graphs, etc.
What symbol denotes pointer dereferencing in C?
The
symbol used for pointer dereferencing in C is the asterisk “*”.
Differentiate between array and pointer.
Aspect |
Array
|
Pointer
|
Definition |
Collection
of elements of same type stored in contiguous memory. |
Variable
that stores the address of another variable. |
Size |
Fixed
at compile time. |
Can
point to dynamically allocated memory. |
Access |
Access
using index: “arr[i]” |
Access
using dereferencing: “(ptr+i)”
|
Memory |
Occupies
memory equal to size × datatype. |
Occupies
memory only for address (usually 4 or 8 bytes). |
Reassignment |
Array
name acts like a constant pointer, cannot be reassigned. |
Pointer
can be reassigned to point elsewhere. |
Compare call-by-value and call-by-reference using pointers.
Aspect Call
by Value
Call
by Reference (using pointers Definition A
copy of the actual argument is passed. Address
of the actual argument is passed.
Effect
on variables Changes
inside function do not affect original variables Changes
inside function affect original variables. Speed Slower
for large data (copy overhead). Faster
for large data (no copy, direct access).