DSA With C Pointer
DSA With C 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
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.
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} };
arr+1
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);
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;
}