Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.

Similar presentations


Presentation on theme: "ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic."— Presentation transcript:

1 ENEE150 – 0102 ANDREW GOFFIN More With Pointers

2 Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic Goffin – ENEE150

3 Arrays as Pointers Arrays are made of data laid out contiguously Array name IS a pointer to the first element in array Goffin – ENEE150 12345678 int a[8] = {1,2,3,4,5,6,7,8}; a *a == 1 and a[0] == 1

4 Pointer Arithmetic Can move through memory by adding to pointers float *a, *p; p = a+1; // how many bytes does this move in memory? If current address is next to address with data, can access this data!  This is the case with arrays, since arrays are stored contiguously.  a[1] == *(a+1) Goffin – ENEE150

5 Pointer Arithmetic Examples An integer pointer, p, stores the address 0x100. An array of length 9 stores the following values: 1,2,3,4,5,6,7,8,9, starting at address 0x100. Answer the following, assuming that an int is 4 bytes: a. What is the address of the number 5 in the array? b. *(p+6) is what value? c. p+3 is what value? d. What is &(p[3])? Goffin – ENEE150

6 Pointer Arithmetic Notes Only makes sense with arrays  The only time values are DEFINITELY stored contiguously in memory  Otherwise, good chance you’ll get garbage Again, *(p+i) is interchangable with p[i]  Also, &(p[i]) == p+i Goffin – ENEE150

7 Passing By Reference Function parameters are inherently “pass by value”  It copies the value itself into the function  This can be computationally expensive for large data types  Also cannot vary value outside function Pass By Reference -> Pass the address  Instead of passing a variable, pass a pointer to a variable!  The pointer itself is passing by value, but you can dereference to get the original variable’s contents  Always a simple 8 bytes (size of pointer)  Can modify contents of address  Arrays are always pass by reference… array names are pointers! Goffin – ENEE150

8 Midterm #1 – 10/6/15 Open note, open book Open Ended Questions and Coding Questions Topics:  C language features  ENEE140  Functional Decomposition  Testing  Debugging  Pointers  More specifics in handout


Download ppt "ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic."

Similar presentations


Ads by Google