Você está na página 1de 22

Pointers in C

Group Members:
© 2010 Tieto Corporation

Kavitha Sreekumar
Nitin Chavan (GL)
Sahitya Gollapalli
Vinay Mandge
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

2 © 2010 Tieto Corporation 2010-03-26


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 Tieto Corporation 2010-03-26


Memory Organisation
Memory Cell Address

0
1
Representation of variable
2

. Quantity Variable
. 150 Value

. 4500 Address

4 © 2010 Tieto Corporation 2011-01-12


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 Tieto Corporation 2010-03-26


Declaration of Pointers
• 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
Use of & and *
• • When is & used?

• • When is * used?

• • & -- "address operator" which gives or produces


the memory address of a data variable.

• * -- "dereferencing operator" which provides the


contents in the memory location specified by a
Pointer.

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);
}
2010-03-26
Pointer and Array
• Declaration :

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

int *nptr ;

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

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


Values 1 2 3 4 5
Address 1000 1002 1004 1006 1008

Base
address
2010-03-26
Pointers to Array
• Declaration :

int *nptr ;
nptr = number[0] ;
or
nptr = number ;

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


value 1 2 3 4 5
address 1000 1002 1004 1006 1008

nptr

10 2010-03-26
Array of Pointers (Ragged arrays)
• Declaration :

char *name[3] = {
“New Zealand” ,
“Australia” ,
“India” ,
}

name[0] New Zealand


name[1] Australia
name[2] India

11 2010-03-26
Pointers to String
Example :

• Declaration : char *cptr = name ;

int length
char *str = “good” ; ;
G O O D \0
while (*cptr != ‘\0’) ;

str length = cptr – name;

12 2010-03-26
Pointers to functions

© 2010 Tieto Corporation


Pointers and Functions
• 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.

14 © 2010 Tieto Corporation 2010-03-26


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

15 © 2010 Tieto Corporation 2010-03-26


Pointer to a function
• 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();
}

16 © 2010 Tieto Corporation 2010-03-26


Pointers and structures

17 © 2010 Tieto Corporation 2010-03-26


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.

© 2010 Tieto Corporation


Some issues with pointers

© 2010 Tieto Corporation


advantages and disadvantages
• Pros
efficiency
convenience
• Cons
error prone
difficult to debug

when not to use pointers?


20 2010-03-26
References
• Programming in ANSI c by E Balaguruswamy.

• need pointers in c ebook by yashwant kanetkar.

2010-03-26
© 2010 Tieto Corporation

Thank You

Você também pode gostar