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

Unit 6

pps unit 6 notes

Uploaded by

Dhruvi Patel
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)
32 views

Unit 6

pps unit 6 notes

Uploaded by

Dhruvi Patel
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/ 11

UNIT 6 Pointers

AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)

Basics of pointers
● Pointers are used for dynamic memory allocation.
● every variable is a memory location and every memory location has its address defined
which can be accessed using ampersand (&) operator, which denotes an address in
memory.
#include <stdio.h>
int main ()
{

int var1;
char var2[10];

printf("Address of var1 variable: %x\n", &var1 );


printf("Address of var2 variable: %x\n", &var2 );

return 0;
}

Output:

● A pointer is a variable whose value is the address of another variable, i.e., direct address
of the memory location. Like any variable or constant, you must declare a pointer before
using it to store any variable address.
● The pointer in C language is a variable which stores the address of another variable. This
variable can be of type int, char, array, function, or any other pointer. The size of the
pointer depends on the architecture. However, in 32-bit architecture the size of a pointer
is 2 byte.

int n = 10;

int* p = &n;
Declaration of a pointer
● The pointer in c language can be declared using * (asterisk symbol). It is also known as
an indirection pointer used to dereference a pointer.
● int *a;//pointer to int
● char *c;//pointer to char

#include<stdio.h>

2
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)

int main(){
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x \n",p); // p contains the address of the number
therefore printing p gives the address of number.

printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a


pointer therefore if we print *p, we will get the value stored at the address contained by p.
return 0;
}

Output:

Null Pointer
● A pointer that is not assigned any value but NULL is known as the NULL pointer. If you
don't have any address to be specified in the pointer at the time of declaration, you can
assign a NULL value. It will provide a better approach.
● int *p=NULL;
● In most libraries, the value of the pointer is 0 (zero).

Pointer Program to swap two numbers without using the 3rd variable.
#include<stdio.h>
int main(){
int a=10,b=20,*p1=&a,*p2=&b;
printf("Before swap: *p1=%d *p2=%d",*p1,*p2);
*p1=*p1+*p2;
*p2=*p1-*p2;
*p1=*p1-*p2;
printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2);
return 0;
}
Output:

3
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)

Advantages of Pointer
● Pointer reduces the code and improves the performance, it is used for retrieving strings,
trees, etc. and used with arrays, structures, and functions.
● We can return multiple values from a function using the pointer.
● It makes you able to access any memory location in the computer's memory.

Pointer to Pointer
● We can also define a pointer to store the address of another pointer. Such a pointer is
known as a double pointer (pointer to pointer).
● The first pointer is used to store the address of a variable whereas the second pointer is
used to store the address of the first pointer.

Syntax:
● int **p; // pointer to a pointer which is pointing to an integer.

C Program for Pointer to Pointer


#include<stdio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a; // pointer p is pointing to the address of a
pp = &p; // pointer pp is a double pointer pointing to the address of pointer p
printf("address of a: %x\n",p); // Address of a will be printed
printf("address of p: %x\n",pp); // Address of p will be printed
printf("value stored at p: %d\n",*p); // value stoted at the address contained by p i.e. 10
will be printed
printf("value stored at pp: %d\n",**pp); // value stored at the address contained by the
pointer stored at pp
}
Output:

4
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)

Pointer and array

Pointer Array

It generally stores the address of another Array usually stores the value of the
variable of the same data type as the pointer variable of the same datatype.
variable's data type.

A pointer to an array can be generated. An array of pointers can be generated.

Pointers are specially designed to store the A normal array stores values of variables,
address of variables. and a pointer array stores the address of
variables.

Usually, arrays can store the number of A pointer variable can store the address
elements the same size as the size of the of only one variable at a time.
array variable.

when the arrays are implemented, the fixed where the pointers are implemented, the
size of the memory block is allocated. memory is dynamically allocated.

pointer to array
● Pointers to an array point to the address of the memory block of an array variable.
● The following is the syntax of array pointers.
● datatype *variable_name[size];
● Here,
● datatype − The datatype of variable like int, char, float etc.
● variable_name − This is the name of variable given by user.
● size − The size of array variable.
C Program
#include<stdio.h>
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int *ptr = arr;
printf("%p\n", ptr);
return 0;
}
Output:

5
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)

What is a pointer? Which arithmetic operations are valid/not valid on pointers?

● A pointer variable in C stores the address of another variable. The address is always an
integer. So, can we perform arithmetic operations such as addition and subtraction on the
pointers? In this chapter, we will explain which arithmetic operators use pointers in C as
operands, and which operations are not defined to be performed with pointers.
● C pointers arithmetic operations are different from the general arithmetic operations.
The following are some of the important pointer arithmetic operations in C:
● Increment and Decrement of a Pointer
● Addition and Subtraction of Integer to Pointer
● Subtraction of Pointers
● Comparison of Pointers

Increment and Decrement of a Pointer


● We know that "++" and "--" are used as the increment and decrement operators in C.
They are unary operators, used in prefix or postfix manner with numeric variable
operands, and they increment or decrement the value of the variable by one.
● Assume that an integer variable "x" is created at address 1000 in the memory, with 10 as
its value. Then, "x++" makes the value of "x" as 11.
int x = 10; // created at address 1000

x++; // x becomes 11

● What happens if we declare "y" as pointer to "x" and increment "y" by 1 (with "y++")?
Assume that the address of "y" itself is 2000.
int x = 10; // created at address 1000

// "y" is created at address 2000


// it holds 1000 (address of "x")
int *y = &x ;

y++; // y becomes 1004


● Since the variable "y" stores 1000 (the address of "x"), we expect it to become 1001
because of the "++" operator, but it increments by 4, which is the size of "int" variable.
● This is because, if the address of "x" is 1000, then it occupies 4 bytes: 1000, 1001, 1002
and 1003. Hence, the next integer can be put only in 1004 and not before it. Hence "y"
(the pointer to "x") becomes 1004 when incremented.

Example of Incrementing a Pointer


#include <stdio.h>

int main(){

int x = 10;
int *y = &x;

6
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)

printf("Value of y before increment: %d\n", y);

y++;

printf("Value of y after increment: %d", y);


}
Output
Value of y before increment: 6422036
Value of y after increment: 6422040

Example of Decrementing a Pointer


#include <stdio.h>

int main(){

double x = 10;
double *y = &x;

printf("value of y before decrement: %ld\n", y);

y--;

printf("value of y after decrement: %ld", y);


}
Output
Value of y before decrement: 6422032
Value of y after decrement: 6422024

● When an array is declared, the elements are stored in adjacent memory locations. In case
of "int" array, each array subscript is placed apart by 4 bytes, as the following figure
shows −

● Hence, if a variable stores the address of 0th element of the array, then the "increment"
takes it to the 1st element.

Example of Traversing an Array by Incrementing Pointer


#include <stdio.h>

int main(){

int a[]= {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int len = sizeof(a)/sizeof(int);
int *x = a;

7
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)

int i = 0;

for(i = 0; i < len; i++){


printf("Address of subscript %d = %d Value = %d\n", i, x, *x);
x++;
}

return 0;
}
Output
Address of subscript 0 = 6421984 Value = 10
Address of subscript 1 = 6421988 Value = 20
Address of subscript 2 = 6421992 Value = 30
Address of subscript 3 = 6421996 Value = 40
Address of subscript 4 = 6422000 Value = 50
Address of subscript 5 = 6422004 Value = 60
Address of subscript 6 = 6422008 Value = 70
Address of subscript 7 = 6422012 Value = 80
Address of subscript 8 = 6422016 Value = 90
Address of subscript 9 = 6422020 Value = 100

Addition and Subtraction of Integer to Pointer


● An integer value can be added and subtracted to a pointer. When an integer is added to a
pointer, the pointer points to the next memory address. Similarly, when an integer is
subtracted from a pointer, the pointer points to the previous memory location.
● Addition and subtraction of an integer to a pointer does not add and subtract that value to
the pointer, multiplication with the size of the data type is added or subtracted to the
pointer.
● For example, there is an integer pointer variable ptr and it is pointing to an address
123400, if you add 1 to the ptr (ptr+1), it will point to the address 123404 (size of an
integer is 4).

ptr = 123400
ptr = ptr + 1
ptr = ptr + sizeof(int)*1
ptr = 123400 + 4
ptr = 123404

Example of Adding Value to a Pointer

In the following example, we are declaring an array and pointer to an array. Initializing the
pointer with the first element of the array and then adding an integer value (2) to the pointer to
get the third element of the array.

#include <stdio.h>

8
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)

int main() {
int int_arr[] = {12, 23, 45, 67, 89};
int *ptrArr = int_arr;

printf("Value at ptrArr: %d\n", *ptrArr);

// Adding 2 in ptrArr
ptrArr = ptrArr + 2;

printf("Value at ptrArr after adding 2: %d\n", *ptrArr);

return 0;
}
Output
Value at ptrArr: 12
Value at ptrArr after adding 2: 45

Example of Subtracting Value to a Pointer

In the following example, we are declaring an array and pointer to an array. Initializing the
pointer with the last element of the array and then subtracting an integer value (2) from the
pointer to get the third element of the array.

#include <stdio.h>

int main() {
int int_arr[] = {12, 23, 45, 67, 89};
int *ptrArr = &int_arr[4]; // points to last element

printf("Value at ptrArr: %d\n", *ptrArr);

// Subtracting 2 in ptrArr
ptrArr = ptrArr - 2;

printf("Value at ptrArr after adding 2: %d\n", *ptrArr);

return 0;
}
Output
Value at ptrArr: 89
Value at ptrArr after adding 2: 45

Subtraction of Pointers

● We are familiar with the "+" and "−" operators when they are used with regular numeric
operands. However, when you use these operators with pointers, they behave in a little
different way.

9
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)

● Since pointers are fairly large integers (especially in modern 64-bit systems), addition of
two pointers is meaningless. When we add a 1 to a pointer, it points to the next location
where an integer may be stored. Obviously, when we add a pointer (itself a large integer),
the location it points may not be in the memory layout.
● However, subtraction of two pointers is realistic. It returns the number of data types that
can fit in the two pointers.

Example of Subtracting Two Pointers

Let us take the array in the previous example and perform the subtraction of pointers of a[0] and
a[9]

#include <stdio.h>

int main(){

int a[]= {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int *x = &a[0]; // zeroth element
int *y = &a[9]; // last element

printf("Add of a[0]: %ld add of a[9]: %ld\n", x, y);


printf("Subtraction of two pointers: %ld", y-x);

}
Output
Add of a[0]: 140729162482768 add of a[9]: 140729162482804
Subtraction of two pointers: 9
It can be seen that the numerical difference between the two integers is 36; it suggests that the
subtraction is 9, because it can accommodate 9 integers between the two pointers.

Comparison of Pointers
● Pointers may be compared by using relational operators such as "==", "<", and ">". If
"p1" and "p2" point to variables that are related to each other (such as elements of the
same array), then "p1" and "p2" can be meaningfully compared.
Example of Comparing Pointers

In the following example, we are declaring two pointers and initializing them with the first and
last elements of the array respectively. We will keep incrementing the first variable pointer as
long as the address to which it points is either less than or equal to the address of the last element
of the array, which is "&var[MAX − 1]" (i.e., the second pointer).

#include <stdio.h>

const int MAX = 3;

int main() {
int var[] = {10, 100, 200};

10
AHMEDABAD INSTITUTE OF TECHNOLOGY PPS(BE01000121)

int i, *ptr1, *ptr2;

// Initializing pointers
ptr1 = var;
ptr2 = &var[MAX - 1];

while (ptr1 <= ptr2) {


printf("Address of var[%d] = %p\n", i, ptr1);
printf("Value of var[%d] = %d\n", i, *ptr1);

/* point to the previous location */


ptr1++;
i++;
}

return 0;
}
Output

Run the code and check its output −

Address of var[0] = 0x7ffe7101498c


Value of var[0] = 10
Address of var[1] = 0x7ffe71014990
Value of var[1] = 100
Address of var[2] = 0x7ffe71014994
Value of var[2] = 200

11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy