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

Pointers Unit 4th 2nd Sem

The document discusses pointers in C and C++. It defines a pointer as a variable that stores the address of another variable. It describes how to declare pointers, initialize them by storing the address of a variable, and use dereferencing operator (*) to access the value of the referenced variable. The document also explains various arithmetic operations that can be performed on pointers like incrementing, decrementing, adding, and subtracting and their usage to traverse arrays and access memory locations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
216 views

Pointers Unit 4th 2nd Sem

The document discusses pointers in C and C++. It defines a pointer as a variable that stores the address of another variable. It describes how to declare pointers, initialize them by storing the address of a variable, and use dereferencing operator (*) to access the value of the referenced variable. The document also explains various arithmetic operations that can be performed on pointers like incrementing, decrementing, adding, and subtracting and their usage to traverse arrays and access memory locations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Programming With C &C++

Unit 4

Pointers

What is Pointer in C?
➢ The Pointer is a variable that stores address of another variable.
➢ A pointer can also be used to refer to another pointer function.
➢ A pointer can be incremented/decremented, i.e., to point to the next/
previous memory location.
➢ The purpose of pointer is to save memory space and achieve faster
execution time.

Declaring a Pointer
A pointer declaration has the following form.

data_type * pointer_variable_name;

Here,

• data_type is the pointer's base type of C's variable types and indicates
the type of the variable that the pointer points to.
• The asterisk (*: the same asterisk used for multiplication) which is
indirection operator, declares a pointer.

Let's see some valid pointer declarations in this C pointers tutorial:

int *ptr_thing; /* pointer to an integer */


int *ptr1,thing;/* ptr1 is a pointer to type integer and thing is an integer varia
ble */
double *ptr2; /* pointer to a double */
float *ptr3; /* pointer to a float */
char *ch1 ; /* pointer to a character */
float *ptr, variable;/*ptr is a pointer to type float and variable is an ordinary
float variable */

Page 1 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++

Initialize a pointer
After declaring a pointer, we initialize it like standard variables with a variable
address. If pointers in C programming are not uninitialized and used in the
program, the results are unpredictable and potentially disastrous.

To get the address of a variable, we use the ampersand (&)operator, placed


before the name of a variable whose address we need. Pointer initialization is
done with the following syntax.

Pointer Syntax

pointer = &variable;

A simple program for pointer illustration is given below:

#include <stdio.h>
int main()
{
int a=10; //variable declaration
int *p; //pointer variable declaration
p=&a; //store address of variable a in pointer p
printf("Address stored in a variable p is:%x\n",p); //accessing the address
printf("Value stored in a variable p is:%d\n",*p); //accessing the value
return 0;
}

Output:

Address stored in a variable p is:60ff08


Value stored in a variable p is:10

Operator Meaning

* Serves 2 purpose

1. Declaration of
a pointer
2. Returns the
value of the
referenced
variable

Page 2 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++

& Serves only 1


purpose

• Returns the
address of a
variable

Features of Pointers:

1. Pointers save memory space.


2. Execution time with pointers is faster because data are manipulated
with the address, that is, direct access to
memory location.
3. Memory is accessed efficiently with the pointers. The pointer assigns
and releases the memory as well. Hence it can be said the Memory of
pointers is dynamically allocated.
4. Pointers are used with data structures. They are useful for
representing two-dimensional and multi-dimensional
arrays.
5. An array, of any type can be accessed with the help of pointers,
without considering its subscript range.
6. Pointers are used for file handling.
7. Pointers are used to allocate memory dynamically.
8. In C++, a pointer declared to a base class could access the object of a
derived class. However, a pointer to a derived class cannot access the
object of a base class.

Uses of pointers:

1. To pass arguments by reference


2. For accessing array elements
3. To return multiple values
4. Dynamic memory allocation
5. To implement data structures
6. To do system level programming where memory addresses are useful

Page 3 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++

Arithmetic Operations with Pointers in C


We can perform arithmetic operations on the pointers like addition,
subtraction, etc.,

However, as we know that pointer contains the address, the result of an


arithmetic operation performed on the pointer will also be a pointer if the
other operand is of type integer.

In pointer-from-pointer subtraction, the result will be an integer value.


Following arithmetic operations are possible on the pointer in C language:

o Increment
o Decrement
o Addition
o Subtraction
o Comparison

Incrementing Pointer in C
If we increment a pointer by 1, the pointer will start pointing to the
immediate next location.

This is somewhat different from the general arithmetic since the value of
the pointer will get increased by the size of the data type to which the
pointer is pointing.

The Rule to increment the pointer is given below:

1. new_address= current_address + i * size_of(data type)

Where i is the number by which the pointer get increased.

32-bit

For 32-bit int variable, it will be incremented by 2 bytes.

64-bit
For 64-bit int variable, it will be incremented by 4 bytes.
Page 4 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++

Let's see the example of incrementing pointer variable on 64-bit


architecture.

1. #include<stdio.h>

2. int main(){

3. int number=50;

4. int *p;//pointer to int

5. p=&number;//stores the address of number variable

6. printf("Address of p variable is %u \n",p);

7. p=p+1;

8. printf("After increment: Address of p variable is %u \n",p); // in our case, p w

ill get incremented by 4 bytes.

9. return 0;

10. }

Output

Address of p variable is 3214864300


After increment: Address of p variable is 3214864304

Traversing an array by using pointer

1. #include<stdio.h>

2. void main ()

3. {

4. int arr[5] = {1, 2, 3, 4, 5};

5. int *p = arr;

6. int i;

7. printf("printing array elements...\n");

8. for(i = 0; i< 5; i++)

9. {

10. printf("%d ",*(p+i));

11. }

12. }

Page 5 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++

Output

printing array elements...


1 2 3 4 5

Decrementing Pointer in C
Like increment, we can decrement a pointer variable. If we decrement a
pointer, it will start pointing to the previous location. The formula of
decrementing the pointer is given below:

1. new_address= current_address - i * size_of(data type)


32-bit
For 32-bit int variable, it will be decremented by 2 bytes.

64-bit
For 64-bit int variable, it will be decremented by 4 bytes.

Let's see the example of decrementing pointer variable on 64-bit OS.

1. #include <stdio.h>

2. void main(){

3. int number=50;

4. int *p;//pointer to int

5. p=&number;//stores the address of number variable

6. printf("Address of p variable is %u \n",p);

7. p=p-1;

8. printf("After decrement: Address of p variable is %u \n",p); // P will now poin

t to the immidiate previous location.

9. }

Output

Address of p variable is 3214864300


After decrement: Address of p variable is 3214864296

Page 6 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++

C Pointer Addition
We can add a value to the pointer variable. The formula of adding value
to pointer is given below:

1. new_address= current_address + (number * size_of(data type))


32-bit
For 32-bit int variable, it will add 2 * number.

64-bit

For 64-bit int variable, it will add 4 * number.

Let's see the example of adding value to pointer variable on 64-bit


architecture.

1. #include<stdio.h>

2. int main(){

3. int number=50;

4. int *p;//pointer to int

5. p=&number;//stores the address of number variable

6. printf("Address of p variable is %u \n",p);

7. p=p+3; //adding 3 to pointer variable

8. printf("After adding 3: Address of p variable is %u \n",p);

9. return 0;

10. }

Output

Address of p variable is 3214864300


After adding 3: Address of p variable is 3214864312

As you can see, the address of p is 3214864300. But after adding 3 with
p variable, it is 3214864312, i.e., 4*3=12 increment. Since we are using
64-bit architecture, it increments 12. But if we were using 32-bit
architecture, it was incrementing to 6 only, i.e., 2*3=6. As integer value
occupies 2-byte memory in 32-bit OS.

Page 7 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++

C Pointer Subtraction
Like pointer addition, we can subtract a value from the pointer variable.
Subtracting any number from a pointer will give an address. The formula
of subtracting value from the pointer variable is given below:

1. new_address= current_address - (number * size_of(data type))


32-bit

For 32-bit int variable, it will subtract 2 * number.

64-bit
For 64-bit int variable, it will subtract 4 * number.

Let's see the example of subtracting value from the pointer variable on
64-bit architecture.

1. #include<stdio.h>

2. int main(){

3. int number=50;

4. int *p;//pointer to int

5. p=&number;//stores the address of number variable

6. printf("Address of p variable is %u \n",p);

7. p=p-3; //subtracting 3 from pointer variable

8. printf("After subtracting 3: Address of p variable is %u \n",p);

9. return 0;

10. }

Output

Address of p variable is 3214864300


After subtracting 3: Address of p variable is 3214864288

You can see after subtracting 3 from the pointer variable, it is 12 (4*3)
less than the previous address value.

Page 8 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++

However, instead of subtracting a number, we can also subtract an


address from another address (pointer). This will result in a number. It
will not be a simple arithmetic operation, but it will follow the following
rule.

If two pointers are of the same type,

1. Address2 - Address1 = (Subtraction of two addresses)/size of data type which

pointer points

Consider the following example to subtract one pointer from an another.

1. #include<stdio.h>

2. void main ()

3. {

4. int i = 100;

5. int *p = &i;

6. int *temp;

7. temp = p;

8. p = p + 3;

9. printf("Pointer Subtraction: %d - %d = %d",p, temp, p-temp);

10. }

Output

Pointer Subtraction: 1030585080 - 1030585068 = 3

Page 9 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++

Illegal arithmetic with pointers


There are various operations which can not be performed on pointers.
Since, pointer stores address hence we must ignore the operations which
may lead to an illegal address, for example, addition, and multiplication. A
list of such operations is given below.

o Address + Address = illegal

o Address * Address = illegal

o Address % Address = illegal

o Address / Address = illegal

o Address & Address = illegal

o Address ^ Address = illegal

o Address | Address = illegal

o ~Address = illegal

Page 10 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali

You might also like