200 likes | 507 Views
C Language. Pointers. Overview of Pointers. A variable that contains the address of another variable Uses concept of indirection Use a complicated syntax, particularly pointers to functions and pointers to pointers Useful for: Complex data structures (linked lists, trees)
E N D
C Language Pointers
Overview of Pointers • A variable that contains the address of another variable • Uses concept of indirection • Use a complicated syntax, particularly pointers to functions and pointers to pointers • Useful for: • Complex data structures (linked lists, trees) • Data passing with functions • Handling arrays more efficiently • Dynamic memory allocation
Simple Variable int counter counter address in memory (e.g. 0xF0001000) value
Concept of Indirection cPtr “points to” counter int counter int *cPtr
Pointer Variable int *cPtr address in memory (e.g. 0xF0002000) cPtr value
Have Already Used Pointers • Array names are pointers to the array int student[ MAX ]; student[ 0 ] Student student[ 1 ] student[ 2 ] “Student” used alone points to the array student[ MAX-1 ]
Pointer Declarations • int *iptr // pointer to an int • char *cptr // pointer to a char • struct entry *ep // pointer to a struct entry
Pointer Initialization • Need to get the address of a data object in order to point to it. • Use the & operator • & returns a pointer to a data object • And now you can understand why scanf requires & operators for simple data types but not for array names
Pointer Initialization Examples • int count; int *iptr = &count; • char text[20]; char *cptr = &text[0]; cp = text; • struct student newStudent; struct student *studentptr = &newStudent
Pointer Initialization Figure 1 uninitialized pointer, doesn’t point to anything yet int counter int *cptr
Pointer Initialization Figure 2 cptr “points to” counter int counter store address of counter in cptr int *cptr = &counter
Pointer Usage Suppose given int *cptr; When use cptr without a ‘*’ we are referring to the address When use *cptr, we are referring to the value of the thing cptr points to
Pointer Usage Examples • int *counterptr*counterptr behaves like an int variable *counterptr++;x = 5 + *counterptr; • char *charptr*charptr behaves like a char variable
Pointer Usage Figure 1 counterPtr “points to” counter int counter int *counterPtr = &counter;
Pointer Usage Figure 2 counterPtr “points to” counter int counter same effect as counter = 20 *counterPtr = 20
Structure Member Reference • use structure pointer operator ( -> ) • ep->member • preferred method of member access • (*ep).member • another method for member access
Structure Member Reference struct employee { char name [ 30 ]; int age; float salary; } employees[10 ]; void print_ages (void) { struct employee *ep; for (ep = &employees[0]; ep < &employees[10]; ep++) { printf (“Age is %d\n”, ep->age); } }
Null Pointer • The value 0 (or NULL from stdio.H) • Specifies a pointer that isn’t pointing to anything
Valid Pointer Operations • Can assign pointers of the same type or 0 (NULL) • Can add or subtract integer values from pointers (useful for handling arrays) • Can compare two pointers for greater than, equals, or less than
Pointer Operations struct employee employees[10]; struct employee *eptr1, *eptr2; eptr1 = ep2; ep2 = NULL; eptr1++; eptr2 += 3; // bad since eptr2 == NULL if (eptr1 < eptr2) eptr1 = &employees[0]; eptr2 = &employees[5]; eptr1 * eptr2 assign add integer compare BAD!