1 / 20

C Language

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)

ilario
Télécharger la présentation

C Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Language Pointers

  2. 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

  3. Simple Variable int counter counter address in memory (e.g. 0xF0001000) value

  4. Concept of Indirection cPtr “points to” counter int counter int *cPtr

  5. Pointer Variable int *cPtr address in memory (e.g. 0xF0002000) cPtr value

  6. 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 ]

  7. Pointer Declarations • int *iptr // pointer to an int • char *cptr // pointer to a char • struct entry *ep // pointer to a struct entry

  8. 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

  9. Pointer Initialization Examples • int count; int *iptr = &count; • char text[20]; char *cptr = &text[0]; cp = text; • struct student newStudent; struct student *studentptr = &newStudent

  10. Pointer Initialization Figure 1 uninitialized pointer, doesn’t point to anything yet int counter int *cptr

  11. Pointer Initialization Figure 2 cptr “points to” counter int counter store address of counter in cptr int *cptr = &counter

  12. 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

  13. Pointer Usage Examples • int *counterptr*counterptr behaves like an int variable *counterptr++;x = 5 + *counterptr; • char *charptr*charptr behaves like a char variable

  14. Pointer Usage Figure 1 counterPtr “points to” counter int counter int *counterPtr = &counter;

  15. Pointer Usage Figure 2 counterPtr “points to” counter int counter same effect as counter = 20 *counterPtr = 20

  16. Structure Member Reference • use structure pointer operator ( -> ) • ep->member • preferred method of member access • (*ep).member • another method for member access

  17. 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); } }

  18. Null Pointer • The value 0 (or NULL from stdio.H) • Specifies a pointer that isn’t pointing to anything

  19. 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

  20. 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!

More Related