0% found this document useful (0 votes)
115 views7 pages

Pointer in C .

Uploaded by

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

Pointer in C .

Uploaded by

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

Chapter-8

Pointer

A pointer is a fundamental concept in programming languages, including C. In


simple terms, a pointer is a variable that holds the memory address of another
variable.

In other words, a pointer "points" to the memory location of another variable. By


using pointers, we can indirectly access and manipulate the value of the variable
being pointed to.

for Ex:

int x=5;

int *ptr=&x;

In above example, x is a variable which hold 5 numeric value and when memory
address of x is passes to pointer *ptr, then *ptr manipulate the value hold by x
variable.

What is the relation between array and pointer?

An array and a pointer are closely related. In fact, an array can be seen as a special
kind of pointer.

In C, when we declare an array, we are essentially creating a contiguous block of


memory that can hold multiple elements of the specified type. The name of the
array acts as a constant pointer to the first element of the array.

int arr[5];

The variable arr represent the memory address of first element i.e. arr[0].

int *ptr=arr;
Then using pointer, we can access the all element of array. *(ptr+0) means array
element of a[0],*(ptr+1) means second array element of a[1],*(ptr+2) means third
array element of a[2]. Similary ,*ptr pointer access all the element of array.

for example:

#include<stdio.h>
int main(){
int i;
int arr[5]= {2,3,4,5,6};
int *ptr=arr;
for(i=0;i<5;i++){
printf("%d\t",*(ptr+i));
}
return 0;
}

output: 2 3 4 5 6

What is void pointer?

A void pointer in C is a special type of pointer that can hold the address of an
object of any type. It is a generic pointer that does not have any specific type
associated with it. It is declared using the void * type.

For example:

void *ptr;

Since a void pointer does not have a specific type associated with it, we cannot
directly dereference a void pointer or perform pointer arithmetic on it. Before using
a void pointer, it needs to be explicitly cast to the appropriate type.
What is null pointer?

A null pointer is a special value that a pointer variable does not point to a valid
memory address. In other words, a null pointer does not point to any specific
object or memory location.

Null pointers are commonly used to indicate the absence of a valid object or to
initialize a pointer before assigning it a valid memory address. They are
particularly useful in conditional statements and error handling.

int* ptr = NULL; // ptr is a null pointer

In this code, ptr is initialized as a null pointer. It does not point to any valid
memory location.

for example :

What is the output of following code?

#include<stdio.h>
int main(){
if(!NULL){
printf("C programming is easy");
}
else {
printf(" c programming is not easy");
}
return 0;
}

Output: C programming is easy


In the if condition, !NULL is used. Since NULL represents a null pointer, !NULL
evaluates to true. That means any non-zero value is considered true in a conditional
statement.Inside the if block, the statement printf("C programming is easy"); is
executed, which prints "C programming is easy" to the console.
Pointer to function:

A pointer to a function, also known as a function pointer, is a variable that holds


the address of a function. It allows you to treat functions as data and manipulate
them dynamically during program execution.

example: to sum two integer value using function pointer:

#include<stdio.h>
int sum(int a, int b) {
return a + b;
}

int main() {
int (*sum_ptr)(int, int);
sum_ptr = sum;
// Calling the function through the function pointer
int result = sum_ptr(3, 5); // result will be 8
printf("the result is %d",result);
return 0;
}

output: the result is 8

Reason of using Pointer:


A pointer enables us to access a variable that is defined outside the function.
They are used to pass the array to function.
Pointers are more efficient in handling the data tables.
Tutorial:
Write a program to sort the array element using pointer as arguments in user
defined function.
#include <stdio.h>
void read(int*,int);
void Sort(int*,int);
void printArray(int*,int);

int main() {
int arr[50],size;
printf("enter the size of array:");
scanf("%d",&size);
read(arr,size);
printf("Original array: ");
printArray(arr,size);
Sort(arr,size);
printf("Sorted array: ");
printArray(arr, size);
return 0;
}
void read(int *arr,int size){
int i;
printf("enter the element of array:");
for(i=0;i<size;i++){
scanf("%d",arr+i);
}
}

void Sort(int *arr, int size) {


for (int i = 0; i < size - 1; i++) {
for (int j =i+1; j < size; j++) {
if (*(arr+i) > *(arr+j)) {
// Swap elements
int temp = *(arr+i);
*(arr+i)=*(arr+j);
*(arr+j)= temp;
}
}
}
}

void printArray(int* arr, int size) {

for (int i = 0; i < size; i++) {


printf("%d ",*(arr+i));
}

printf("\n");
}

#write a program in c to find the second largest element of array using


pointer.
#include <stdio.h>
void read(int*,int);
void Sort(int*,int);
int main() {
int arr[50],size;
printf("enter the size of array:");
scanf("%d",&size);
read(arr,size);

Sort(arr, size);
printf("The second largest element is %d:",arr[size-
2]);

return 0;
}

void read(int *arr,int size){


int i;
printf("enter the element of array:");
for(i=0;i<size;i++){
scanf("%d",arr+i);
}
}

void Sort(int *arr, int size) {


for (int i = 0; i < size - 1; i++) {
for (int j =i+1; j < size; j++) {
if (*(arr+i) > *(arr+j)) {
// Swap elements
int temp = *(arr+i);
*(arr+i)=*(arr+j);
*(arr+j)= temp;
}
}
}
}

You might also like