new ppt sahi

22
© 2010 Tieto Corporation Pointers in C Group Members: Kavitha Sreekumar Nitin Chavan (GL) Sahitya Gollapalli Vinay Mandge

Transcript of new ppt sahi

Page 1: new ppt sahi

© 2

010

Tie

to C

orpo

ratio

n

Pointers in C

Group Members:

Kavitha SreekumarNitin Chavan (GL)Sahitya GollapalliVinay Mandge

Page 2: new ppt sahi

© 2010 Tieto Corporation

Contents

• Introduction to Pointer

• Pointer advantages

• Declaration of Pointers

• Pointer Arithmetic

• Pointers to Arrays and strings

• Pointers and Structures

• Pointers to functions

• Some issues with pointers

2010-03-262

Page 3: new ppt sahi

© 2010 Tieto Corporation

Introduction to Pointer ?

• Derived data type

• “A pointer is a variable, which contains the address of

another variable.”

• In other words:

- A variable that holds Memory Address rather than its

content.

3 2010-03-26

Page 4: new ppt sahi

© 2010 Tieto Corporation 2011-01-124

Memory Cell

0

1

.

.

.

2

Quantity Variable

150 Value

4500 Address

Representation of variable

Memory OrganisationAddress

Page 5: new ppt sahi

© 2010 Tieto Corporation

Pointer advantages

• To point to different data structures.• To achieve clarity and simplicity.• More compact and efficient coding.• To return multiple values via functions.• Dynamic memory allocation.

5 2010-03-26

Page 6: new ppt sahi

• Variable name must be preceded by Asterisk (*)

• Should have some data type

• E.g.:-

• Declaration :-

int *ptr; // indicates ptr is pointer to an integer variable.

• Initialize :-

ptr= &x; // ptr is pointing to x.

2010-03-26

Declaration of Pointers

Page 7: new ppt sahi

Use of & and *• • When is & used?

• • When is * used?

• • & -- "address operator" which gives or producesthe memory address of a data variable.

• * -- "dereferencing operator" which provides thecontents in the memory location specified by aPointer.

2010-03-26

Page 8: new ppt sahi

2010-03-26

Pointer Arithmetic

• Operations such as Increment & Decrement.

• Ptr++ ; // Points to next memory location.

• E.g.:-

# include <stdio.h>

char movie[] =“Jurassic park”;

main()

{

char *ptr;

ptr=movie;

printf(“%s”,movie);

printf(“%s”,ptr);

ptr++;

printf(“%s”,movie);

printf(“%s”,ptr);

}

Page 9: new ppt sahi

• Declaration :

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

int *nptr ;

nptr = &x[0]; // Points to address location 1000.

2010-03-26

Pointer and Array

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

1 2 3 4 5

1000 1002 1004 1006 1008

Elements

Values

Address

Base address

Page 10: new ppt sahi

Pointers to Array • Declaration : int *nptr ; nptr = number[0] ; or nptr = number ;

2010-03-2610

1 2 3 4 5

1000 1002 1004 1006 1008

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

value

address

nptr

Page 11: new ppt sahi

Array of Pointers (Ragged arrays)• Declaration : char *name[3] = { “New Zealand” , “Australia” , “India” , }

2010-03-2611

name[0]

name[1]

name[2]

New Zealand

Australia

India

Page 12: new ppt sahi

Pointers to String

• Declaration :

char *str = “good” ;

2010-03-2612

G O O D \0

str

Example : char *cptr = name ;

int length ;

while (*cptr != ‘\0’) ;

length = cptr – name;

Page 13: new ppt sahi

© 2010 Tieto Corporation

Pointers to functions

Page 14: new ppt sahi

© 2010 Tieto Corporation

Pointers and Functions

14 2010-03-26

• Pointers can be used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there.This is known as "call by reference" since we are referencing the variables.

• As shown swap function modified from a "call by value" to a "call by reference". Note that the values are now actually swapped when the control is returned to main function.

Page 15: new ppt sahi

© 2010 Tieto Corporation

Pointers with Functions (example)

15 2010-03-26

#include <stdio.h>void swap ( int a, int b ) ;int main ( ){ int a = 5, b = 6; printf("a=%d b=%d\n",a,b) ; swap (a, b) ; printf("a=%d b=%d\n",a,b) ; return 0 ;}

void swap( int a, int b ){ int temp; temp= a; a= b; b = temp ; printf ("a=%d b=%d\n", a, b);}Results:

a=5 b=6a=6 b=5a=5 b=6

Page 16: new ppt sahi

© 2010 Tieto Corporation

Pointer to a function

16 2010-03-26

• Pointer to function are the pointers which have the ability to call the function pointed to by them. As the ordinary pointers take the address of variable and can change the value pointed by them, the function pointer almost does the same.

• example• #include <stdio.h>

#include <conio.h>int mul(int a, int b, int c) {  return a*b*c;}void main() {  int (*function_pointer)(int, int, int);  function_pointer = mul;  printf("The product of three numbers is:%d", function_pointer(2, 3, 4));  getch();}

Page 17: new ppt sahi

© 2010 Tieto Corporation

Pointers and structures

17 2010-03-26

Page 18: new ppt sahi

© 2010 Tieto Corporation

Pointers to Structures • We declare pointers to structures the same way we declare

any other pointers: by preceding the variable name with an asterisk in the declaration. We could declare two pointers to struct complex with

• struct complex *p1, *p2; And, as before, we could set these pointers to point to actual variables of type complex:

• p1 = &c2; p2 = &c3; Then, • *p1 = *p2 would copy the structure pointed to by p2 to

the structure pointed to by p1 (i.e. c3 to c2), and • p1 = p2would set p1 to point wherever p2 points.

Page 19: new ppt sahi

© 2010 Tieto Corporation

Some issues with pointers

Page 20: new ppt sahi

advantages and disadvantages

• Pros efficiency convenience• Cons error prone difficult to debug

2010-03-2620when not to use pointers?

Page 21: new ppt sahi

References• Programming in ANSI c by E Balaguruswamy.

• need pointers in c ebook by yashwant kanetkar.

2010-03-26

Page 22: new ppt sahi

© 2

010

Tie

to C

orpo

ratio

n

Thank You