You are on page 1of 24

Course Title: Applied Computer

Programming II

Course Code: GEC 225

Omega Semester
2019/2020 Session
Module 4
Arrays and Pointers
Module 4b
Pointers
4b.1 What is a pointer
• A pointer is a variable that stores the address of
another variable.
• The addresses are the location number always
contains whole number, therefore pointer contains
whole number.
• It is called pointer because it points to a particular
location in memory by storing the address of that
location.
• Pointers are used in C program to access the
memory and manipulate the address.

4 Monday, April 27,2020


4b.2 What is a pointer (cont)
• Remember, variable is a storage ,every variable is a
memory location and every memory location has its
address.

• Like any variable or constant, you must declare a


pointer before using it to store any variable address.

5 Monday,April 27, 2020


4b.3 What to know about pointer
• when to use a pointer
• when to dereference the pointer
• when to pass an address of a variable rather than
the variable itself
• when to use pointer arithmetic to change the
pointer
• how to use pointers without making your programs
unreadable.

6 Monday, April 27,2020


4b.4 Pointer Declaration
The general form of a pointer variable declaration is
Syntax-
Data type *pointer name;
The * before pointer shows the compiler that variable
declared is a pointer.
e.g.
int *p1; //pointer to integer type
float *p2; //pointer to float type
char *p3; //pointer to character type
When pointer is declared, it contains garbage value i.e.
it may point to any value in the memory.

7 Monday ,April 27,2020


4b.5 Operators used in pointer
Two operators are used in the pointer namely address
operator(&) also known as ampersand (&) operator
and indirection operator or dereference operator (*).

The ampersand (&) operator is commonly called the


reference operator and denotes an address in memory.

8 Monday, April, 27, 2020


Figure 1 : block of 2 bytes at address 204 stores variable a.

9 Wednesday, May 6, 2020


4b.6 Pointer Expression
From figure 1 :
 P is storing address of a.

 Using some operators on p we can reach a.

 P also takes some memory , so p is stored at location


64, and it also takes 2 bytes.

10 Monday, April, 27, 2020


4b.7 Pointer Expression (cont)
so p is a pointer variable that points to an integer, to
store the address of a in p we need to use, the
statement below:
p = &a;
where &a is the address of a. This shows that p has the
address of a and points to a, assigning a value to a.
By using * operator we can access the value of a
variable through a pointer.

11 Monday, April, 27, 2020


4b.8 Pointer Expression (cont)
Example 1:
int a = 10.;
int *p;
p = &a;
printf("%d", *p);
The output will be 10
Similarly if we assign a value to *pointer like this:
*p = 200;
It would change the value of variable a. The statement
above will change the value of a from 10 to 200.

12 Monday, April, 27, 2020


4b.9 Expression (cont)
Example 2:
int x = 1, y = 2, z[10];
int *ip; // ip is a pointer to an int, so it can point to x, y, or
an element of z
ip = &x; // ip points at the location where x is stored
y = *ip; // set y equal to the value pointed to by ip, or y = x
*ip = 0; // now change the value that ip points to to 0, so
now x = 0
// but notice that y is unchanged
ip = &z[0]; // now ip points at the first location in the array z

*ip = *ip + 1; // the value that ip points to (z[0]) is incremented

13 Monday, April, 27, 2020


4b.10 Pointer Expression (cont)
Example 3:
#include <stdio.h>
int main()
{
int var = 40;

int *p;
OUTPUT
p= &var; Value of variable var is: 40
Value of variable var is: 40
Address of variable var is: 0x7fff5ed98c4c
printf("Value of variable var is: %d", var); Address of variable var is: 0x7fff5ed98c4c
Address of pointer p is: 0x7fff5ed98c50
printf("\nValue of variable var is: %d", *p);
printf("\nAddress of variable var is: %p", &var);
printf("\nAddress of variable var is: %p", p);
printf("\nAddress of pointer p is: %p", &p);
return 0;
}

14 Monday, April, 27, 2020


4b.11 Pointer Arithmetic
Pointer arithmetic is different from ordinary
arithmetic and it is perform relative to the data type.

If integer pointer contain address of 2000 on


incrementing we get address of 2002 instead of 2001,
because, size of the integer is of 2 bytes.

15 Monday, April, 27, 2020


4b.12 Pointer Arithmetic (cont)
Arithmetic operation never perform on pointer are:

addition, multiplication and division of two pointer.


multiplication between the pointer by any number.
division of pointer by any number
add of float or double value to the pointer.

16 Monday, April, 27, 2020


4b.13 Pointer Arithmetic (cont)
For 16 bit machine 32 bits
The size of a pointer to an integer is 2 4
The size of a pointer to long is 4 8
The size of a pointer to float is 4 8
The size of a pointer to char is 1 2
The size of a pointer to double is 8 16

Operations performed in pointer are:

/* Addition of a number through pointer */

Example 4: ( assume 32 bits)


int i=100;
int *p; // p is a pointer to int i,
p=&i; //p points at the location where i is stored
p=p+2; //increase the address that p point to by 8
p=p+3; //increase the address that p point to by 12
p=p+9; //increase the address that p point to by 36

17 Monday, April, 27, 2020


4b.14 Pointer Arithmetic (cont)
Example 5:

1. #include<stdio.h>
2. int main(){
3. int num=70;
4. int *x;//pointer to int
5. x=&num ;//stores the address of num variable
6. printf("Address of x variable is %u \n",x);
7. x=x+1;
8. printf("After increment: Address of x variable is %u \n",x); // in our case, x will get
incremented by 4 bytes.
9. return 0;
10. }

OUTPUT
Address of x variable is 1239347716
After increment: Address of x variable is 1239347720

18 Monday, April, 27, 2020


4b.15 Pointer Arithmetic (cont)
/* Subtraction of a number from a pointer’*/
Example 6 :

1. #include<stdio.h>
2. int main(){
3. int num=70;
4. int *x;//pointer to int
5. x=&num ;//stores the address of num variable
6. printf("Address of x variable is %u \n",x);
7. x=x-3; //subtracting 3 from pointer variable
8. printf(" subtracting 3: Address of x variable is %u \n",x);
9. return 0;
10. }
OUTPUT
Address of x variable is 3917007556
subtracting 3: Address of x variable is 3917007544

19 Monday, April, 27, 2020


4b.16 Using pointers with arrays
Example 7:This program prints address of each individual element of an
array.

#include <stdio.h>
int main() OUTPUT
{ &x[0] = 6356724
int x[6]; &x[1] = 6356728
int i; &x[2] = 6356732
for(i = 0; i < 6; ++i)
{
&x[3] = 6356736
printf("&x[%d] = %u\n", i, &x[i]); &x[4] = 6356740
} &x[5] = 6356744
printf("Address of array x: %u", x); Address of array x: 6356724
return 0; Notice that, printing &x[0]
}
and x gave us the same
result.

20 Monday, April, 27, 2020


Consider an array [5]
Int x[5]
x X[o] X[1] X[2] X[4] X[5]
4b.17 Using pointers with arrays (cont)
Recall in example 2, we did ip = &z[0];
This sets our pointer to point at the first element of the array.
From example 7, it's clear that x and &x[0] both contains the
same address. Hence, &x[0] is equivalent to x.
And, x[0] is equivalent to *x.
Similarly,
&x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).
&x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2).

22 Monday, April, 27, 2020


4b.18 Using pointers with arrays (cont)
Example 8 : array example using pointer

1. int x[10] = {12, 20, 39, 43, 54, 60, 70, 79, 89, 96}, *y;
2. y = &x[0]; // y points to the beginning of the array
3. printf("%d\n", x[0]); // outputs 12
4. printf("%d\n", *y); // also outputs 12
5. printf("%d\n", *y+1); // outputs 13 (12 + 1)
6. printf("%d\n", (*y)+1); // also outputs 13
7. printf("%d\n", *(y+1)); // outputs x[1] or 20
8. y+=2; // y now points to x[2]
9. printf("%d\n", *y); // prints out 39
10. *y = 38; // changes x[2] to 38
11. printf("%d\n", *y-1); // prints out x[2] - 1 or 37
12. *y++; // sets y to point at the next array element
13. printf("%d\n", *y); // outputs x[3] (43)
14. (*y)++; // sets what y points to to be 1 greater
15. printf("%d\n", *y); // outputs the new value of x[3] (44)

23 Monday, April, 27, 2020


Module 4b Assignment
QUESTION ONE:
1. #include <stdio.h>
2. int main() OUTPUT:
3. { WHAT IS THE OUTPUT OF THE CODE IN
4. int x[7] = {5, 7, 9, 11, 13, 15, 17}; QUESTION ONE.

5. int* ptr;
6. ptr = &x[3];
7. printf("*ptr = %d \n", *ptr);
8. printf("*(ptr+1) = %d \n", *(ptr+1));
9. printf("*(ptr-1) = %d \n", *(ptr-1));
10. printf("*(ptr-2) = %d \n", *(ptr-2));
11.
12. return 0;
13. }

QUESTION TWO : (a)How can Memory address be accessed using


pointer.
(b) How can the value of a variable be accessed through a pointer.
Note:
This assignment should be submitted on Moodle latest by Friday 15th
May, 2020.

24 Monday, April 27, 2020

You might also like