Sie sind auf Seite 1von 22

POINTERS

27/03/06

Declaration a point about pointers


int j, k, l; j, k, l are integer type. we can say that int type declaration distributes over the list of names j, k, l int *p, q, r; Here, only p is int pointer q and r are ordinary int variables The indirection operator (*) does not distribute to all variable names in a declaration. Each pointer must be declared with the * prefixed to the name.
2

27/03/06

Pointing to nothing !

NULL is a symbolic constant defined in <stdio.h> with 0 (zero) A pointer with the value NULL points to nothing. The value 0 (NULL) is the only integer value that can be assigned directly to a pointer variable. int *p = NULL; *p = 5; /* this results in run-time error */
3

27/03/06

Functions

A function can return only one value back to the calling function. If you want more values to be communicated back to the calling function, then you have to use addresses and pointers.

27/03/06

Functions an example
void find_min_max_marks(float *, float *, float *, int); main( ) { float min, max, marks[40]; ; find_min_max_marks(marks, &min, &max, 40); } void find_min_max_marks(float *m, float *a, float *z, int size) { int j; *z = m[0]; *a = m[0]; for (j=1; j<size; j++) { if(m[j] < *a) *a = m[j]; if(m[j] > *z) *z = m[j]; } }
27/03/06 5

Comparing two strings

strcmp(s, t) is a function available in the standard library (<string.h>) that compares the character strings s and t. It returns an integer which is

0 (zero) s and t are same Negative s is lexicographically less than t Positive s is lexicographically larger than t

The return value is obtained by subtracting the characters at the first position where s and t disagree.
6

27/03/06

strcmp array version


int strcmp(char *s, char *t) { int j; for(j=0; s[j] == t[j]; j++) if( s[j] == \0) return 0; return s[j] t[j] ; }

27/03/06

strcmp pointer version


int strcmp( char *s, char *t) { for ( ; *s == *t; s++, t++) if(*s == \0) return 0; return *s - *t; }

27/03/06

const with pointers

Passing addresses to functions could be dangerous. By mistake that function can modify the original variables value. To overcome this kind of problems, one can declare that the contents pointed by the pointer are constant. One can also force to say that a pointer has to point to the same location (whatever be contents of the location).
9

27/03/06

const with pointers

const can be used in 4 ways with pointers


a non-constant pointer to non-constant data a constant pointer to non-constant data a non-constant pointer to constant data a constant pointer to constant data

27/03/06

10

A non-constant pointer to nonconstant data

int *p, j = 10, k = 20; p = &j; *p = 15; /* contents pointed by p is modified */ p = &k; /* p is modified */

27/03/06

11

A constant pointer to non-constant data

Pointer always points to the same memory location. But contents at that memory can change.

int x, y; int * const ptr = &x; *ptr = 7; /* OK same as x = 7*/ ptr = &y; /* error */

27/03/06

12

A constant pointer to non-constant data


An array name is a constant pointer to the beginning of the array. All the data in the array can be accessed and changed by using the array name and array subscripting. But array name cannot point to some other location.

27/03/06

13

A non-constant pointer to constant data


pointer can be modified to point to some other location. But the contents using the pointer (with indirection operator *) can not be modified. This is especially useful with functions.

27/03/06

14

A non-constant pointer to constant data

const int * ptr; int j = 10, k; ptr = &j; /* OK */ *ptr = 25; /* error */ j = 25; /* OK */ ptr = &k; /* OK */ *ptr = 100; /* error */ k = 100; /* OK */ j = *ptr; /* OK */
15

27/03/06

A non-constant pointer to constant data

void f( const int *); int main( ) { int y = 111; f(&y); } void f( const int * ptr) { *ptr = 100; /* an error occurs */ }

27/03/06

16

A constant pointer to constant data

int x = 5, y; const int *const ptr = &x; y = *ptr; /* OK */ ptr = &y; /* error */ *ptr = 20; /* error */ If you want to pass an array whose contents should not be modified, then use this kind of declarations.

27/03/06

17

Pointer Expressions and Pointer Arithmetic

A limited set of arithmetic operations may be performed with pointers

A pointer may be incremented (++) or decremented ( -- ) An integer may be added to a pointer (+ or += ) An integer may be subtracted from a pointer ( or = ) One pointer may be subtracted from another.

Pointer arithmetic is machine dependant.


18

27/03/06

Increment ++ and decrement -

int *ptr, j = 5; int a[20]; ptr = a; ptr ++; /* ptr now points to a[1] */ *ptr = 10; /* same as a[1] = 10 */ ptr = &j; ptr ++; /* now ptr points to next integer after j */ Pointer arithmetic is meaningful only with arrays
19

27/03/06

Subtracting a pointer from other

double d[20]; double *a, *b; int j; a = &d[4]; b = &d[10]; j = b a; /* j gets value 6 */ Again, it would be meaningless if a and b are pointing to non-array elements.

27/03/06

20

Assigning a pointer to another Void pointer


A pointer can be assigned to another pointer if both pointers are of the same type. Otherwise, a type cast operator must be used. int *p; char *c;
p = (int *) c;

The exception to this rule is for pointer whose type is void *


21

27/03/06

void pointer

void *ptr; int i; ptr = &i; Here ptr can contain an address, but the type of the value stored at that address is unknown and hence, dereferencing using ptr is disallowed. *ptr = 5; /* error */ *((int *)ptr) = 5; /* Ok, type cast and then assign*/
22

27/03/06

Das könnte Ihnen auch gefallen