35
POINTERS

Pointers

Embed Size (px)

DESCRIPTION

ppt on pointers in c

Citation preview

Page 1: Pointers

POINTERS

Page 2: Pointers

It is one of the most distinct and most important features of C language.

It has added power and flexibility to the language.

Pointers are powerful and very handy tool.

The benefits are:

1. Pointers are more efficient in handling arrays.

2. It can be used to return multiple values from a function via function arguments.

3. Pointers permit references to functions and thereby facilitating passing of functions as arguments to other functions.

4. It allow to support dynamic memory management.

5. It reduce length and complexity of programs.

6. They increase the execution speed and thus reduce the program execution time.

Of course, the real power of C lies in the proper use of pointers.

Page 3: Pointers

Understanding pointersMemory organisation

Memory cell

0

1

2

3

:

:

:

65,535

Address

Page 4: Pointers

int n;

n= 30;

Variable

Value

Address

Memory cell

:

:

250

251

:

:

:

65,535

Address

n 30n

30

251P= 250

Page 5: Pointers

Pointer is a variable which holds the address of another variable.

It points to another variable.

Page 6: Pointers

Accessing the address of a variable

int n;

P = &n;

P=250

& Address operator (Address of)

Page 7: Pointers

WAP to print the address of a variable#include <stdio.h>

void main()

{

int a;

float b;

a=300;

b=20.25;

clrscr();

printf("\nThe address of the variable a is %u",&a);

printf("\nThe address of the variable b is %u",&b);

getch();

}

Page 8: Pointers

Declaration of pointer variable Syntax:

data_type *pt_name;

This tells the compiler that

1. The asterisk (*) tells that the variable pt_name is a pointer.

2. pt_name needs a memory location.

3. pt_name points to a variable of type data_type.

Eg:

int *p;

float *x;

Page 9: Pointers

Initialization of pointer variables

The process of assigning the address of a variable to a pointer variable is known as initialization.

All the pointer variables should be initialized before they are used in the program.

int n;

int *p; declaration

n=30;

p= &n; initialization

int * p= &n;

Page 10: Pointers

float a, b;

int x, *p;

p= &a;

b= *p;

WRONG

Page 11: Pointers

int x, *p = x;

int *p = &x, x;

int *p=NULL;

int *p=0;

int *p = 5360;

Page 12: Pointers

Accessing a variable through its pointer Once a pointer has been assigned the address of a variable, the

value of a variable can be accessed by using a variable.

It is done by * (asterisk) operator.

* is known as indirection operator or dereferencing operator.

int qty, *p,n;

qty=256;

p=&qty;

n=*p;

*p = 5;

n = 256n = 5

Page 13: Pointers

Pointer flexibilityint x,y,z,*p;

............

p=&x;

............

p=&y;

............

p=&z;

............

x y z

p

Page 14: Pointers

Pointer flexibilityint x;

int *p=&x;

int *q=&x;

int *r=&x;p q r

x

Page 15: Pointers

POINTERS AND ARRAYS

When an array is declared, compiler allocates sufficient amount of memory to contain all the elements of the array.

Base address which gives location of the first element is also allocated by the compiler. Suppose we declare an array arr,

int arr[5]= {7,4,8,9,6};

Assuming that the base address of arr is 1000 and each integer requires two byte, the five element will be stored as follows

Page 16: Pointers

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.

arr is equal to &arr[0]

int *p;

p = arr;

or p = &arr[0];

Successive elements can be retrieved by incrementing a pointer (p++).

POINTERS AND ARRAYS

1000P

Page 17: Pointers

#include <stdio.h>void main(){ int i; int a[5] = {1, 2, 3, 4, 5}; int *p = a; // same as int*p = &a[0]

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

printf(“%d\n ", *p); p++; }

To access memory elements using a pointer

Page 18: Pointers

int x[5];int *p;p=&x;1 2 3 4 5

X[0] X[1] X[2] X[3] X[4]

1000 1002 1004 1006 1008

Elements

Values

Address

The relationship between x and p is:

p=&x[0]

p+1= &x[1]

p+2= &x[2]

p+3= &x[3]

p+4= &x[4]

(= 1002)

(= 1004)

(= 1006)

(= 1008)

(= 1000)

Page 19: Pointers
Page 20: Pointers

WAP to accept n array elements and display using pointer

printf("\nThe array elements are\n");

for(i=0;i<n;i++)

{

printf("%d\n",*ptr);

ptr++;

}

getch();

}

#include <stdio.h>

void main()

{

int a[30];

int i,n,*ptr;

ptr=a;

printf("Enter the value of n : ");

scanf("%d",&n);

printf("\nEnter the array elements");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

Page 21: Pointers

Pointers and strings

char str[5]=“HELL0”;

Or

char *str;

str=“Hello”

or

char *str=“HELLO”;

Page 22: Pointers

Program to accept and display a string using pointer

#include <stdio.h>

void main()

{

char *s;

clrscr();

printf("Enter the string : ");

gets(s);

printf("The enterd sting is %s",s);

getch();

}

Page 23: Pointers

Wap to find the length using pointer ( without strlen() )

#include <stdio.h>

void main()

{

char *s;

int len=0;

clrscr();

printf("Enter the string : ");

gets(s);

while(*s)

{ s++;

len++

}

printf("The length is %d",len);

getch();

}

Page 24: Pointers

Wap to find the length using pointer ( without strlen() )

#include <stdio.h>

void main()

{

char *s;

int len;

char *ptr;

ptr=s;

printf("Enter the string : ");

gets(s);

while(*ptr)

{ ptr++;

}

len=ptr-s;

printf("The length is %d",len);

getch();

}

Page 25: Pointers

PASSING ARGUMENTS TO FUNCTIONS

The mechanism used to convey information to the function is the ‘argument’.

Arguments (Parameters) are passed to function when calling a function.

The parameters passed to the function are called as actual parameters. (Parameters used in function call)

Parameters given in function definition are called formal parameters.

Page 26: Pointers

Function Arguments:

While calling a function, there are two ways that arguments can be passed to a function:

Pass by value

Pass by reference

Page 27: Pointers

CALL BY VALUE (PASS BY VALUE)

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function.

In this case, changes made to the parameter inside the function have no effect on the argument.

Call by value is restricted to one-way transfer of information.

It allows transfer of only zero or one value only through the return mechanism.

 

Page 28: Pointers

#include <stdio.h>

void change(int);

void main()

{

int n;

clrscr();

printf("Enter the values of n : ");

scanf("%d",&n);

printf("\nThe value of n before function call is %d",n);

change(n);

printf("\nThe value of n after function call is %d",n);

getch();

}

void change(int n)

{

n=n+30;

printf("\nThe value of n within function is %d",n);

}

Page 29: Pointers

LAB: WAP to show pass by valuevoid swap(int a, int b)

{

int temp;

temp = a;

a= b;

b = temp;

}

#include <stdio.h>

void swap(int x, int y);

int main ()

{

// local variable declaration:

int a,b;

clrscr();

printf("Enter two values of a and b : ");

scanf("%d %d",&a,&b);

printf("Before swap, value of a : %d", a);

printf("\nBefore swap, value of b : %d",b);

swap(a, b);

printf("\nAfter swap, value of a :%d",a);

printf("\nAfter swap, value of b : %d", b);

getch();

return 0;

}

Page 30: Pointers

PASS BY REFERENCE (CALL BY REFERENCE) The call by pointer method of passing arguments to a function

copies the address of an argument into the formal parameter.

Inside the function, the address is used to access the actual argument used in the call.

This means that changes made to the parameter affect the passed argument.

When pointers are used as arguments asterisk (*) must precede the formal pointer argument.

Page 31: Pointers

#include <stdio.h>

void change(int *n);

void main()

{

int n;

clrscr();

printf("Enter the values of n : ");

scanf("%d",&n);

printf("\nThe value of n before function call is %d",n);

change(&n);

printf("\nThe value of n after function call is %d",n);

getch();

}

void change(int *n)

{

*n=*n+30;

}

Page 32: Pointers

LAB: WAP to implement pass by reference

#include <stdio.h>

void swap(int *a, int *b);

int main ()

{

// local variable declaration:

int a,b;

clrscr();

printf("Enter two values of a and b : ");

scanf("%d %d",&a,&b);

printf("Before swap, value of a : %d", a);

printf("\nBefore swap, value of b : %d",b);

swap(&a,&b);

printf("\nAfter swap, value of a :%d",a);

printf("\nAfter swap, value of b : %d", b);

getch();

return 0;

}

void swap(int *a, int *b)

{

int temp;

temp = *a;

*a= *b;

*b = temp;

}

Page 33: Pointers

#include <stdio.h>

void concatenate_string(char*, char*);

int main(){ //char original[100], add[100]; char *str1,*str2; clrscr(); printf("Enter source string\n"); gets(str1);

printf("Enter string to concatenate\n"); gets(str2);

concatenate_string(str1,str2); printf("String after concatenation is %s", str1); getch();

return 0;}

void concatenate_string(char *str1, char *str2){ while(*str1) str1++;

while(*str2) { *str1 = *str2; str2++; str1++; } *str1 = '\0';}

LAB: WAP to concatenate strings using pointers

Page 34: Pointers

Pointer Arithmetic

Pointer can be:

i) Incremented

ii) Decremented

iii) a value can be added

iv) a value can be decremented

int a=50;

int *p;

p=&a;

p++;

p--;

p-2;

p+5;

50

a

2004

2004

5000

p

2006

2004

Page 35: Pointers

Array of pointers char name[5][30];

char *name[5];

n