1 / 23

Pointers

Pointers. Pointers. Pointers permit a program to work with addresses of variables rather than just their values. Powerful feature of the C language Also treacherous. Easy to make mistakes Mistakes can be very difficult to track down. You have to be very careful with pointers!.

Download Presentation

Pointers

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

  2. Pointers • Pointers permit a program to work with addresses of variables rather than just their values. • Powerful feature of the C language • Also treacherous. • Easy to make mistakes • Mistakes can be very difficult to track down. • You have to be very careful with pointers!

  3. Why would we want to do this? • Necessary in system programming • Operating systems, device drivers, etc. • Permit a function to modify caller’s non-array data • Like we are able to do with arrays • Permit a function to return more than one value to the caller. • Can be of various types. • Can have meaningful names.

  4. Why would we want to do this? • Required for complex data structures • Permit a function to access complex data structures in caller’s address space.

  5. Pointer Variables • Pointer variables are specific to a type int *pCount; // better: int* pCount, but … • The * means that pCount is a pointer to an int. i.e., it can contain the address of an int *pCount represents the value that pCount points to. The * in *pCount is the dereferencing operator. *pCount sometimes called the target of the pointer • We use *pCount like a normal int variable. • But we have to be sure that pCount is set to point to an actual integer variable. • This does not happen automatically!

  6. Setting Pointer Variables • Pointer variables have to be set to the address of a something before being used. • To get the address of a variable, put & in front of the name. • As for scanf • Example int value; int *pValue; .... pValue = &value;

  7. Using a Pointer int main () { int value = 100; int *pValue; printf ("Before increment, value = %d\n", value ); pValue = &value; *pValue += 1; printf ("After increment, value = %d\n", value ); return 0; } Set pointer pValue to address of value Increment the int variable that pValue points to

  8. Dereferencing a Pointer • Effectively replaces the pointer by the variable to which it points. • If pValue is a pointer to an int, *pValue is an int • Equivalent to using the name of the variable whose address is in pValue. • * has very high precedence • like all unary operators.

  9. Using a Pointer

  10. Using a Pointer • We could have used *pValue in the printf #include <stdio.h> int main () { int value = 100; int * pValue; pValue = &value; // Set pointer before using it printf ("Before increment, value = %d\n", *pValue); *pValue += 1; printf ("After increment, value = %d\n", *pValue); return 0; } Same effect as value.

  11. Same result. Using a Pointer

  12. Using a Pointer • Pointers to other types work the same double *pRadius; char *pSomeChar;

  13. Using a Pointer Summary If pValue is a pointer to int *pValue works like a normal int variable • Use on either side of an assignment *pValue = some_other_int; some_other_int = *pValue; • Use in function calls printf (“Value is %d\n”, *pValue);

  14. Pointers as Function Arguments

  15. Passing Pointers to Functions • Functions can be defined with addresses as parameters. void Get_Sides (int *width, int *length) • Caller passes addresses of variables that the function can then access. • Address &value • Pointer pValue

  16. Function with Address Parameters void Get_Sides (int *width, int *length) { do { *width = -1; printf ("Please enter Width as positive integer: "); scanf ("%d", width); /* Clear input buffer. Only do this if user */ /* is expected to input only one value per line*/ while (getchar() != '\n'); if (*width <= 0) { printf ("Received invalid value %d\n", *width); } } while (*width <= 0); /* Same for length */ /* ... */ } No & (width is an address) Check value entered by user No “return” statement. Values are put directly into variables whose addresses are passed as arguments.

  17. Passing Addresses as Arguments int main (void) { int width = 0; int length = 0; int area = 0; printf ("This program computes the area of a rectangle\n"); Get_Sides (&width, &length); area = width * length; printf ("Area is %d\n", area); return 0; } Must pass in the location of the arguments,i.e., their addresses

  18. Program Running

  19. Program Running

  20. Passing Addresses as Arguments • We could also have passed pointers as arguments to Get_Sides.

  21. Passing Pointers as Arguments int main (void) { int width = 0; int length = 0; int *pWidth = &width; int *pLength = &length; int area; printf ("This program computes the area of a rectangle\n"); Get_Sides (pWidth, pLength); area = width * length; printf ("Area is %d\n", area); return 0; } No & this time! Effect is identical to previous example.

  22. A Good Test Question int main (void) { int *pWidth; int *pLength; int area; printf ("This program computes the area of a rectangle\n"); Get_Sides (pWidth, pLength); area = *pWidth * *pLength; printf ("Area is %d\n", area); return 0; } What’s wrong with this program?

  23. Compiles without error Runtime error (Good!) Try It!

More Related