DOC PREVIEW
U-M EECS 281 - Pointers – Array dereferencing, type independence

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

11Pointers – Array dereferencing, type independence● Array subscript operator ( [] ) is a form of indexeddereferencing.● C++ pointers are strictly typed; can't have an intpointer point to a char pointer, for example.:int myInt = 42;int* pMyInt = &myInt;char c = 'c'char* pMyChar = &c;pMyInt = pMyChar;12Pointers – Typecasting● We can, however, cast one data type to another::int myInt = 42;int* pMyInt = &myInt;char c = 'c'char* pMyChar = &c;// pMyInt = pMyChar;pMyInt = reinterpret_cast< int* >( pMyChar );● There are several typecasts you can do.reinterpret_cast could be interpreted as the mostdangerous. static_cast is safer, but there aresituations in which it won't do.● Old C-style casts like pMyInt = (int*)pMyChar will stillwork, though. These are like reinterpret_cast<>.13Pointers and new● Of course, pointers start to become really useful withnew, when we allocate memory on the heap::int* pMyIntAry = new int[1000 ];pMyIntAry[ 25 ] = 1001;// ... do some interesting stuff heredelete[] pMyIntAry;OSCodeStackData/HeapMemory layout:14Pointers to pointers (to pointers...)● We can also have pointers to pointers::int myInt = 42;int* pMyInt = &myInt;int** ppMyInt = &pMyInt;// int*** pppMyInt = &ppMyInt;● Question: when might this be useful?● Question: could we do something like int** ppMyInt =&&myInt ? What about int myInt2 = **ppMyInt ?● Answer: implementing dynamic multi-dimensional arrays,implementing pass-by-reference for pointers, arrays ofstrings, command-line options e.g. main (int argc, char **argv)● Answer: &&myInt will generate an error, because you can'ttake the address of an address. **ppMyInt will work,though. It evaluates to myInt. (What about *ppMyInt ?)15Pointer arithmetic:● When we increment or decrement (or add or subtract from)pointers, we're changing them by a factor of the size of thepointed-to data type, in bytes.● For example, assume char is one byte wide, float is four byteswide, and double is eight bytes wide. Also assume that (note:these are the addresses, not the dereferenced values):char* myCharPtr == 1000float* myFloatPtr == 2000double* myDoublePtr == 3000● Let's change each of the pointers. Specifically, we do:--myCharPtr;++myFloatPtr;myDoublePtr += 5;● What are the new (address) values of the pointers?16Pointer arithmetic:char* myCharPtr == 999float* myFloatPtr == 2004double* myDoublePtr == 3040--myCharPtr;++myFloatPtr;myDoublePtr += 5;● The new values are:17Addenda : post-incrementing vs. pre-incrementing:#include <stdlib.h>#include <stdio.h>int main(int argc, char **argp){ int x = 5; printf("x=%d\n", ++x); printf("x=%d\n", x++); printf("x=%d\n", x); return EXIT_SUCCESS;}● There is a semantic difference between pre-incrementing and post-incrementing. Observe the following C code:● For pre-increment, the x is incremented before being “used”,whereas with post-increment, x is incremented a fter being


View Full Document

U-M EECS 281 - Pointers – Array dereferencing, type independence

Download Pointers – Array dereferencing, type independence
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Pointers – Array dereferencing, type independence and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Pointers – Array dereferencing, type independence 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?