Beruflich Dokumente
Kultur Dokumente
27/03/06
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
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
27/03/06
27/03/06
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
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
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
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
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
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
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
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
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
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.
27/03/06
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
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
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;
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