7_Arrays_Pointers

7_Arrays_Pointers - Pointers and Arrays A QUICK PREVIEW OF...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPE13 Cyrus Bazeghi A QUICK PREVIEW OF FOR C HAPTERS 10 AND 11 Pointers and Arrays
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CMPE13 P OINTERS AND A RRAYS We have briefly seen these before, here are the details Pointer Address of a variable in memory Allows us to indirectly access variables in other words, we can talk about its address rather than its value Array A list of values arranged sequentially in memory Example: a list of telephone numbers Expression a[4] refers to the 5th element of the array a 2 2
Background image of page 2
CMPE13 A DDRESS VS . V ALUE Sometimes we want to deal with the address of a memory location, rather than the value it contains. Adding a column of numbers. bob contains address of first location. Read value, add to sum, and increment bob until all numbers have been processed. bob is a pointer -- it contains the address of data we‟re interested in. x3107 x2819 x0110 x0310 x0100 x1110 x11B1 x0019 x3100 x3101 x3102 x3103 x3104 x3105 x3106 x3107 x3100 bob address value 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CMPE13 A NOTHER N EED FOR A DDRESSES Consider the following function that's supposed to swap the values of its arguments. void Swap(int firstVal, int secondVal) { int tempVal = firstVal; firstVal = secondVal; secondVal = tempVal; } 4
Background image of page 4
CMPE13 E XECUTING THE S WAP F UNCTION firstVal secondVal valueB valueA 3 4 4 3 R6 before call tempVal firstVal secondVal valueB valueA 3 4 3 4 3 R6 after call These values changed. .. ...but these did not. Swap needs addresses of variables outside its own activation record. Swap main 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CMPE13 P OINTERS IN C C lets us talk about and manipulate pointers as variables and in expressions. Declaration int *p; /* p is a pointer to an int */ A pointer in C is always a pointer to a particular data type: int* , double* , char* , etc. Operators *p -- returns the value pointed to by p &z -- returns the address of variable z 6
Background image of page 6
CMPE13 E XAMPLES int i; int *ptr; i = 4; ptr = &i; *ptr = *ptr + 1; store the value 4 into the memory location associated with i store the address of i into the memory location associated with ptr read the contents of memory at the address stored in ptr store the result into memory at the address stored in ptr 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CMPE13 P OINTERS AS A RGUMENTS
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 08/23/2010.

Page1 / 26

7_Arrays_Pointers - Pointers and Arrays A QUICK PREVIEW OF...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online