ch 7-pointers

Post on 20-Jan-2015

1.309 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Pointerc code

TRANSCRIPT

1

Chapter-7

Pointers

2

PointersA pointer is a variable that holds a memory address.

This address is the location of another object in memory.

For example, if one variable contains the address of another variable, the first variable is said to point to the second.

Basically, a pointer contains an address of another variable.

Pointers provide an indirect means of accessing or retrieving the data from memory.

3

Pointers

Variable in memory

1000

1001

1002

1003

1004

1005

1006

103

Memory address

4

The ‘&’ and ‘*’

Consider the declaration int ab=3;

This declaration tells the compiler

a) Reserve space in memory to hold the integer value.

b) Associate the name ab with this memory location.

c) Store the value 3 at this location.

5

The ‘&’ and ‘*’

3

ab

1000

Location name

Value at Location

Address

6

Declaring pointer variable • In C every variable must be declared for its

data type. Since pointer variables contain addresses that belong to a separate data type they must be declared as pointers.

• The declaration of pointer variable takes following form:

data_type *variable_name;

For example:- int *q; here q is pointer variable that can hold the address

7

The ‘&’ and ‘*’

‘&’ Address of operator

‘*’ Value at address operator. Also called the Indirection Operator.

‘&a’ Returns the address of variable a.

‘*a’ Returns the value stored in a particular address.

8

An Example:Pointersmain()

{

int j=5;

printf(“Address of j=%d\n”,&j); printf(“Value of j=%d\n”,j);

printf(“Value of j=%d\n”,*(&j));

} Output:

Address of j = 1000

Value of j = 5

Value of j = 5

#include <stdio.h>

int main()

{ int a = 10, b = 2;

int *p1, *p2;

p1 = &a; p2 = &b;

printf("%u %u \n", p1, p2);

printf("%d %d \n", *p1, *p2);

return 0;

} 9

A program that prints out pointers and the values they point to:

10

Write a program that computes the area and perimeter of a rectangle:

#include <stdio.h>void rectangle(int a, int b, int * area, int * perim);int main(){ int x, y; int area, perim; printf("Enter two values separated by space: " ); scanf("%d %d", &x, &y); rectangle(x, y, &area, &perim); printf("Area is %d Perimeter is %d\n", area, perim); return 0;}void rectangle(int a,int b,int * area,int * perim){ *area = a * b; *perim = 2 * (a + b);}

11

Pointer Expressions

Expressions involving pointers can be classified as follows:

• Pointer Assignments

• Pointer Arithmetic

• Pointer Comparison

• Pointer Conversion

12

Pointer Assignments

Consider the following example

i

5

1000

1000

2000

j

Here i’s value is 5 and j’s value is i’s address.

13

Pointer Assignments

Since, the variable j is containing an address, it is declared as int *j;int *j;

This declaration tells the compiler that j will be used to store the address of an integer value – i.e j points to an integer.

14

An Example:Pointer Assignmentsmain(){

int j=3;int *k;k = &j;printf(“Address of j=%d”,&j);printf(“Address of j=%d”,k);printf(“Address of j=%d”,&k);printf(“Value of k=%d”,k);printf(“Value of j=%d”,j);printf(“Value of j=

%d”,*(&j));printf(“Value of j=%d”,*j);

}

Output:

Address of j = 1000

Address of j = 1000

Address of k = 2000

Value of k = 1000

Value of j = 3

Value of j = 3

Value of j = error

15

Pointer Conversions

One type of pointer can be converted to another type of pointer.Consider the following example:

main(){

double x = 100.1,y;int *p;

/*The next statement causes p to point to double*/p = (int*)&x;y = *p;printf(“The value of x is: %f”,y);

}

16

Pointers v/s Arrays

Consider the following programmain()

{

static char arr[10]=“Embedded”;

char *s=“Embedded”;

printf(“%s\n”,arr);

printf(“%s\n”,s);

}

Output:

Embedded

Embedded

17

Pointers v/s Arrays

Consider the following programmain()

{

static char arr[10]=“Embedded”;

char *s=“Embedded”;

s++;

printf(“%s\n”,arr);

printf(“%s\n”,s);

}

Output : Embedded

mbedded

18

Analysis: Pointers v/s Arrays

s++ increments s such that it starts pointing to ‘m’ of Embedded and hence would print mbedded as output.

arr++ would give an error message because the information known to us is the base address and arr++ is attempting to change this base address.

19

Arrays of Pointers

As there are an array of ints, floats, similarly there can be an array of pointers.An array of pointers represent a collection of addresses.

20

An Example: Arrays of Pointers

main(){

int *arr[4];int i=5,j=10,k=15,l=20,m;arr[0]=&i;arr[1]=&j;arr[2]=&k;arr[3]=&l;for(m=0;m<=3;m++)

printf(“%d”,*(arr[m]));}

21

Arrays of Pointers

1000 250020001500

i

5 10

j

2015

lk

2500200015001000

arr[0] arr[3]arr[2]arr[1]

1200 270022001700

22

Pointers To Pointers

Pointer as we know is a variable which contains an address of another variable.

A pointer which contains an address of another pointer variable can be defined as a pointer to a pointer or DOUBLE POINTER.

23

Double Pointers

3

i

1000

1000

2000

j

2000

3000

k

24

Double Pointer

main(){

int i=3;int *j;int **k;j=&i;k=&j;printf(“Address of i is %d%d%d\n”,&i,j,*k);printf(“Address of j is %d%d\n”,&j,k);printf(“Address of k is %d\n”,&k);printf(“Value of i is %d%d%d%d\n”,i,*(&i),*j,**k);printf(“Value of j is %d\n”,j);printf(“Value of k is %d\n”,k);

}

25

Double Pointer

OUTPUT:

Address of i is 1000 1000 1000

Address of j is 2000 2000

Address of k is 3000

Value of i is 3 3 3 3

Value of j is 1000

Value of k is 2000

26

SummaryA pointer is a variable that holds a memory address.Basically, a pointer contains an address of another variable.Pointers provide an indirect means of accessing or retrieving the data from memory.Arithmetic operations such as addition, multiplication and division on two pointers are not allowed.A pointer variable cannot be multiplied/divided by a constant or a variable.A pointer which contains an address of another pointer variable can be defined as a pointer to a pointer or DOUBLE POINTER.

top related