1 / 6

Using const with pointers

Using const with pointers. 4 ways. Nonconstant pointer to nonconstant data. The data can be modified through the dereferenced pointer, and the pointer can be modified to point to other data. An example declaration is: int * countPtr. Nonconstant pointer to constant data.

kamil
Download Presentation

Using const with 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. Using const with pointers 4 ways

  2. Nonconstant pointer to nonconstant data The data can be modified through the dereferenced pointer, and the pointer can be modified to point to other data. An example declaration is: int *countPtr

  3. Nonconstant pointer to constant data The pointer can be modified to point to any data item of the appropriate type, but the data to which it points cannot be modified through THAT pointer. An example declaration is: constint *countPtr; read as countPtr is a pointer to an integer constant.

  4. constant pointer to Nonconstant data A pointer that always points to the same memory location; the data at that location CAN be modified through the pointer. An example is an array name, which is a constant pointer to the beginning of the array.

  5. constant pointer to constant data A pointer that always points to the same memory location; the data at that location CANNOT be modified through the pointer. This is how an array should be passed to a function that only reads the array and does not modify it!

  6. Pointer Arithmetic Is pointless unless used on a pointer that points to an array. You cannot assume that two integers, say, are stored next to each other in memory! Subtracting or comparing two pointers that do not refer to elements of the same array is a logic error! A void pointer cannot be dereferenced. An array can be thought of as a constant pointer.

More Related