0% found this document useful (0 votes)
13 views35 pages

Unit 2 - 3

Notes

Uploaded by

sudharsr
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)
13 views35 pages

Unit 2 - 3

Notes

Uploaded by

sudharsr
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/ 35

POINTERS

Definition
A pointer is a variable whose value is the address
of another variable, i.e., direct address of the
memory location. This is done by using unary
operator * that returns the value of the variable
located at the address specified by its operand.
POINTERS
 Syntax
Datatype *pointervariable;

 Syntax Example

1. int *ip; /* pointer to an integer */


2. double *dp; /* pointer to a double */
3. float *fp; /* pointer to a float */
4. char *ch /* pointer to a character */
POINTERS
 Example

int var = 20; /* actual variable declaration */

int *ip; /* pointer variable declaration */

ip = &var; /* store address of var in pointer


variable*/
POINTERS
Reference operator (&) and Dereference operator (*)

1. & is called reference operator. It gives the address of


a variable.

2. * is called dereference operator. It gives the value


from the address
Pointer to Pointer
Double (**) is used to denote the double pointer.
Double Pointer Stores the address of the Pointer
Variable. Conceptually we can have Triple ….. n
pointers.
Example 1
int main()
{
int num = 45 , *ptr , **ptr2ptr ;
ptr = # //3000
ptr2ptr = &ptr; //4000
printf("%d",**ptr2ptr);
return(0);
}
Output 45
Pointer to Constant Objects
These type of pointers are the one which cannot
change the value they are pointing to. This means
they cannot change the value of the variable whose
address they are holding.

const datatype *pointername;


(or)
datatype const *pointername;
Example
The pointer variable is declared as a const. We
can change address of such pointer so that it will
point to new memory location, but pointer to such
object cannot be modified (*ptr).
Example
Constant Pointers
Constant pointers are the one which cannot change address
they are pointing to. This means that suppose there is a pointer
which points to a variable (or stores the address of that
variable). If we try to point the pointer to some other variable,
then it is not possible.

int* const ptr=&variable;


(or)
int *const ptr=&variable // ptr is a constant pointer to int
Null pointer
 NULL Pointer is a pointer which is pointing to nothing.
 Pointer which is initialized with NULL value is considered as
NULL pointer.

datatype *pointer_variable=0;

datatype *pointer_variable=NULL;
Example
Pointer Arithmetic
 C allows you to perform some arithmetic operations on
pointers.
 Incrementing a pointer
Incrementing a pointer is one which increases the number of
bytes of its data type.
int *ptr;
int a[]={1,2,3};
ptr=&a;
ptr++;
ptr=&a;
ptr=ptr+1;
Pointer Arithmetic
 Decrementing a Pointer

Decrementing a pointer is one which decreases the number


of bytes of its data type.

Using Unary Operator


int *ptr;
int a[]={1,2,3};
ptr=&a;
ptr--;
Limitations of Pointer Arithmetic
 Addition of 2 pointers is not allowed

 Addition of a pointer and an integer is commutative ptr+5ó 5+ptr

 Subtraction of 2 pointers is applicable.

 subtraction of a pointer and an integer is not commutative ptr-5≠ 5-ptr.

 Only integers can be added to pointer. It is not valid to add a float or double value
to a pointer.
 A pointer variable cannot be assigned a non address value except zero.

 Multiplication and division Operators cannot be applied on pointers.

 Bitwise operators cannot be applied on pointers.

 A pointer and an integer can be subtracted.

 A pointer and an integer can be added.


Void pointer
1.Void pointer is a generic pointer and can point to any type of
object. The type of object can be char, int, float or any other
type.
 Example
2.A pointer to any type of object can be assigned to a void
pointer.

OUTPUT
3. A void pointer cannot be dereferenced
Relational operations
 A pointer can be compared with a pointer of same type or

zero.

 Various relational operators are ==.,!=,<,<=,>,>=

 Ex: float a=1.0,b=2.0,*fptr1,*fptr2;

 fptr1=&a;fptr2=&b;

 int result;

result=fptr1!=fptr2;
Example
Pointers and Arrays
 Pointers and arrays are closely related , An array variable is
actually just a pointer to the first element in the array.
 Accessing array elements using pointers is efficient way than
using array notation.
 When an array is declared, compiler allocates sufficient
amount of memory to contain all the elements of the array.
 Base address i.e address of the first element of the array is
also allocated by the compiler.
Pointers and Arrays Cont…
Suppose we declare an array arr,
int arr[5]={ 1, 2, 3, 4, 5 };Assuming that the base address of
arr is 1000 and each integer requires two bytes, the five
elements will be stored as follows
Pointers and Arrays Cont…
 Here variable arr will give the base address, which is a

constant pointer pointing to the element, arr[0].

 Therefore arr is containing the address of arr[0] i.e 1000. In

short, arr has two purpose- it is the name of an array and it


acts as a pointer pointing towards the first element in the
array.
Pointers and Arrays Cont…
int *p;
p = arr;
or
p = &arr[0];
Now we can access every element of array arr using p++ to
move from one element to another.
Pointers and Arrays Cont…
 a[0] is the same as *a
 a[1] is the same as *(a + 1)
 a[2] is the same as *(a + 2)
 If pa points to a particular element of an array, (pa + 1)
always points to the next element, (pa + i) points i elements
after pa and (pa - i) points i elements before.
Example 1
#include<stdio.h>
void main()
{
int a[10],i,n;
printf("Enter n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("a[%d]=%d\n",i,*(a+i));
}
Output
Enter n 5
12345
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5
Pointer to Multidimensional Array
 A multidimensional array is of form, a[i][j]. Lets see how we can

make a pointer point to such an array.

 As we know now, name of the array gives its base address. In

a[i][j], a will give the base address of this array, even a+0+0 will
also give the base address, that is the address of a[0][0] element.

 Here is the generalized form for using pointer with

multidimensional arrays.

 *(*(a + i) + j) is same as a[i][j].


Example 2
#include <stdio.h>
#define ROWS 4
#define COLS 3
int main ()
{
int i,j;
// declare 4x3 array
int matrix[ROWS][COLS] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}};
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Output
1 2 3

4 5 6

7 8 9

10 11 12
matrix[0][0] = *(*(matrix))
matrix[i][j] = *((*(matrix)) + (i * COLS + j))
matrix[i][j] = *(*(matrix + i) + j)
matrix[i][j] = *(matrix[i] + j)
matrix[i][j] = (*(matrix + i))[j]
&matrix[i][j] = ((*(matrix)) + (i * COLS + j))
Example 3
void array_of_arrays_ver(int arr[][COLS])
{
int i, j;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
}
void ptr_to_array_ver(int (*arr)[COLS])
{
int i, j;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
printf("%d\t", (*arr)[j]);
}
arr++;
printf("\n");
}
}
Output
Double Pointer
#include <stdio.h>
#define ROWS 4
#define COLS 3
int main ()
{
// matrix of 4 rows and 3 columns
int matrix[ROWS][COLS] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}};
int** pmat = (int **)matrix;
printf("&matrix[0][0] = %u\n", &matrix[0][0]);
printf("&pmat[0][0] = %u\n", &pmat[0][0]);
return 0;
}
Output
&matrix[0][0] = 2675498000

&pmat[0][0] = 1

You might also like