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

DSA With C Pointer

Uploaded by

meetdave642
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

DSA With C Pointer

Uploaded by

meetdave642
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Unit-7 Pointer

1
What is Pointer?
 A normal variable is used to store value.
 A pointer is a variable that store address / reference of another
variable.
 Pointer is derived data type in C language.
 A pointer contains the memory address of that variable as their
value. Pointers are also called address variables because they
contain the addresses of other variables.
 A pointer is a variable whose value is the address of another
variable, i.e., direct address of the memory location.
Declaration & Initialization of Pointer
 Like any variable or constant, you must declare a pointer before using it to store
any variable address.
 The general form of a pointer variable declaration is −
Syntax
datatype *variablename;
 Here, datatype is the pointer's base type; it must be a valid C data type
and variablename is the name of the pointer variable.
 The asterisk * used to declare a pointer is the same asterisk used for
multiplication.
 However, in this statement the asterisk is being used to designate a variable as
a pointer.
int *ip; /* pointer to an integer */
float *fp; /* pointer to a float */
double *dp; /* pointer to a double */
char *cp; /* pointer to a character */
Declaration & Initialization of Pointer
Example Output
void main() 10 10 5000
{
int a=10, *p;
// assign memory address of a to Variable Value Address
pointer variable p
p = &a;
a 10 5000
printf("%d %d %d", a, *p, p);
}
p 5000 5048
 p is integer pointer variable
 & is address of or referencing operator which returns memory
address of variable.
 * is indirection or dereferencing operator which returns value stored
at that memory address. Example::
 & operator is the inverse of * operator pointerdemo.c

 x = a is same as x = *(&a)
Why use Pointer?
 Pointers are useful for accessing memory locations.
 Pointers provide an efficient way for accessing the elements of an
array structure.
 Pointers are used for dynamic memory allocation as well as
deallocation.
 Pointers are used to form complex data structures such as linked
list, graph, tree, etc.
Disadvantages of Pointers in C
 Pointers are a little complex to understand.
 Pointers can lead to various errors such as segmentation faults or
can access a memory location which is not required at all.
 If an incorrect value is provided to a pointer, it may cause memory
corruption.
 Pointers are also responsible for memory leakage.
 Pointers are comparatively slower than that of the variables.
 Programmers find it very difficult to work with the pointers;
therefore it is programmer's responsibility to manipulate a pointer
carefully.
Pointer to Pointer – Double Pointer
 Pointer holds the address of another variable of same type.
 When a pointer holds the address of another pointer then such
type of pointer is known as pointer-to-pointer or double pointer.
 The first pointer contains the address of the second pointer, which
points to the location that contains the actual value.

Syntax
datatype **ptr_variablename;
Pointer Pointer Variable

Example address address value


int **ptr;
Write a program to print variable, address of pointer variable and pointer to pointer variable.

Program
#include <stdio.h>
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
ptr = &var; // address of var
pptr = &ptr; // address of ptr using address of operator &
printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);
return 0;
}

Output
Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000
Relation between Array & Pointer
 When we declare an array, compiler allocates continuous blocks of
memory so that all the elements of an array can be stored in that
memory.
 The address of first allocated byte or the address of first element
is assigned to an array name.
 Thus array name works as pointer variable.
 The address of first element is also known as base address.
Relation between Array & Pointer

#include <stdio.h>

int main()
{
Int x[4];
int i;
for(i = 0; i < 4; ++i)
{
printf("&x[%d] = %p\n", i, &x[i]);
}
printf("Address of array x: %p", x);
return 0;
}

Output
&x[0] = 0060FEEC
Example :: arraypointer.c
&x[1] = 0060FEF0
&x[2] = 0060FEF4
&x[3] = 0060FEF8
Address of array x: 0060FEEC
Relation between Array & Pointer
 There is a difference of 4 bytes between two consecutive elements
of array x. It is because the size of int is 4 bytes (on our compiler).
 Notice that, the address of &x[0] and x is the same. It's because the
variable name x points to the first element of the array.

 From the above example, it is clear that &x[0] is equivalent to x.


And, x[0] is equivalent to *x
Relation between Array & Pointer
include <stdio.h>
int main()
{
int i, x[6], sum = 0;
printf("Enter 6 numbers: ");

for(i = 0; i < 6; ++i)


{
scanf("%d", x+i); // Equivalent to scanf("%d", &x[i]);

sum += *(x+i); // Equivalent to sum += x[i]


}
printf("Sum = %d", sum);
return 0;
}

Enter 6 numbers: 1
2
3
4
5
6
Sum = 21
Pointers and two dimensional Arrays
 Suppose arr is a 2-D array, we can access any element arr[i][j] of
the array using the pointer expression *(*(arr + i) + j).
 Let us take a two dimensional array arr[3][4]:
 int arr[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };

 Since memory in a computer is organized linearly it is not possible


to store the 2-D array in rows and columns.
Pointers and two dimensional Arrays
 We know that the name of an array is a constant pointer that
points to 0th 1-D array and contains address 5000. Since arr is a
‘pointer to an array of 4 integers’,
 according to pointer arithmetic the expression arr + 1 will
represent the address 5016 and expression arr + 2 will represent
address 5032.
 So we can say that arr points to the 0th 1-D array, arr + 1 points to
the 1st 1-D array and arr + 2 points to the 2nd 1-D array(first
element).
Pointers and two dimensional Arrays

• arr -Points to 0th element of arr(Points to 0th 1 D array i.e. 5000)


• arr+1 -Points to 0th element of arr+1(Points to 1st 1 D array i.e.
5016)
• arr+2 -Points to 0th element of arr+2(Points to 2nd 1 D array i.e.
5032)
Pointers and two dimensional Arrays
 In general we can write:
 arr + i Points to ith element of arr -> Points to ith 1-D array

arr+1 

 arr pointer to first row. *(arr+1)+1


 arr+i pointer to ith row.
 *(arr+ i) pointer to first element in ith row.
 *(arr+i)+j pointer jth element in ith Row.
• *(*(arr+i)+j) value stored in the cell(I,j) means ith row and jth col
Program
#include<stdio.h>
Address of 0 th array 0060FEC8
int main() arr[0][0]=11
{ arr[0][1]=22
int arr[3][4] = {
{11,22,33,44},
arr[0][2]=33
{55,66,77,88}, arr[0][3]=44
{11,66,77,44}
};

int i, j; Address of 1 th array 0060FED8


arr[1][0]=55
for(i = 0; i < 3; i++) arr[1][1]=66
{
printf("Address of %d th array %p \n",i , *(arr + i));
arr[1][2]=77
for(j = 0; j < 4; j++) arr[1][3]=88
{
printf("arr[%d][%d]=%d\n", i, j, *( *(arr + i) + j) );
}
printf("\n\n"); Address of 2 th array 0060FEE8
} arr[2][0]=11
arr[2][1]=66
// signal to operating system program ran fine
return 0;
arr[2][2]=77
} arr[2][3]=44
Strings and Pointers
 Similar like arrays, string names are "decayed" to pointers.
 Hence, you can use pointers to manipulate elements of the string.
int main(void) {
char name[] = "Harry Potter";

printf("%c\n", *name); // Output: H


printf("%c\n", *(name+1)); // Output: a
printf("%c\n", *(name+7)); // Output: o

char *namePtr;

namePtr = name;
printf("%c\n", *namePtr); // Output: H
printf("%c\n", *(namePtr+1)); // Output: a
printf("%c\n", *(namePtr+7)); // Output: o
}
Strings and Pointers
 Character array char str[5] =“SVIT”;
 Method to create string using pointer var of type char
 E.g. Char *str=“SVIT”;
 this Create a string for the literal and then store its address in the
pointer var str. The pointer str now points to the first character of
the string.

S V I T \0

str .
WAP using pointer to determine the length of a character
string
#include<stdio.h>
int main()
{
char *name;
Svit
int length;
S is stored at 00403024 address
char *cptr;
v is stored at 00403025 address
name="Svit";
i is stored at 00403026 address
cptr=name;
t is stored at 00403027 address
printf("%s\n", name);
// printf("%s", cptr);
length = 4
while(*cptr != '\0')
{
printf("%c is stored at %p address\n ",*cptr,cptr);
cptr++;
}
length=cptr-name;
printf("\n length = %d", length);
}
Array of Pointer (String)
 Normal Declaration Char name[3][25];
 Now pointer to String
 char* name[3] ={ “India”, “Australia” ,“New Zealand”};
 name[0] ------ > “India”
 name[1] ------ > “Australia”
 name[2] -------- > “New Zealand”

I n d i a ‘ \0’

A u s t r a l i a ‘ \0 ’

N e w Z e a l a n d ‘ \0 ‘
Pointer to Function
 Every function has reference or address, and if we know the
reference or address of function, we can access the function using
its reference or address.
 This is the way of accessing function using pointer.
Syntax
return-type (*ptr-function)(argument list);

 return-type: Type of value function will return.


 argument list: Represents the type and number of value function
will take, values are sent by the calling statement.
 (*ptr-function): The parentheses around *ptr-function tells the
compiler that it is pointer to function.
 If we write *ptr-function without parentheses then it tells the
compiler that ptr-function is a function that will return a pointer.
Sum of two numbers using pointer to function

Program
#include<stdio.h>
int Sum(int,int);
int (*ptr)(int,int);
int main()
{
int a,b,rt; Output
printf("\nEnter 1st number : "); Enter 1st number : 5
scanf("%d",&a);
printf("\nEnter 2nd number : "); Enter 2nd number : 10
scanf("%d",&b);
ptr = Sum; The sum is : 15
rt = (*ptr)(a,b);
printf("\nThe sum is : %d",rt);
return 0;
} Example::Pointer_fu
int Sum(int x,int y) nction.c
{
return x + y;
}

You might also like