Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering Computing I Chapter 5 Pointers and Arrays.

Similar presentations


Presentation on theme: "Engineering Computing I Chapter 5 Pointers and Arrays."— Presentation transcript:

1 Engineering Computing I Chapter 5 Pointers and Arrays

2 Pointers - Introduction A pointer is a variable that contains the address of a variable Pointers are much used in C, because: Pointers are sometimes the only way to express a computation Pointers usually lead to more compact and efficient code Pointers and arrays are closely related Spring 20112Chapter 5

3 Pointers - Introduction Block of Memory Pointer: Contains the address of c The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to Therefore the statement x = *p; and x = c; are equivalent Spring 20113Chapter 5

4 Pointers - Exercise Find the values of x, y, and z[0] through z[9] after the program ends Spring 20114Chapter 5

5 Pointers Manipulations This means the location that dp points to is a double x=x+10; y=x+1; x=x+1; ++x x++ Spring 20115Chapter 5

6 Pointers and Function Arguments Since C passes arguments to functions by value, there is no direct way for the called function to alter a variable in the calling function Swaps ONLY copies of the arguments! Spring 20116Chapter 5

7 Pointers and Function Arguments Passes addresses of the two operands to function Spring 20117Chapter 5

8 Pointers and Arrays Spring 20118Chapter 5

9 Pointers and Arrays *pa  a[0] *(pa+1)  a[1] *(pa+2)  a[2] Spring 20119Chapter 5

10 Pointers and Arrays Spring 201110Chapter 5

11 Pointers and Arrays Spring 201111Chapter 5

12 Pointers and Arrays Preferred Declaration Spring 201112Chapter 5

13 Address Arithmetic If p is a pointer to some element of an array, then: p++ increments p to point to the next element p+=i increments it to point i elements beyond where it currently does. Spring 201113Chapter 5

14 Address Arithmetic If p and q point to members of the same array, then: relations like ==, !=, =, etc., work properly For example, p < q is true if p points to an earlier element of the array than q does Spring 201114Chapter 5

15 Character Pointers and Functions char *pmessage; pmessage = "now is the time"; assigns to pmessage a pointer to the character array. This is not a string copy; only pointers are involved. C does not provide any operators for processing an entire string of characters as a unit Spring 201115Chapter 5

16 Character Pointers and Functions amessage is an array pmessage is a pointer Spring 201116Chapter 5

17 Character Pointers and Functions Spring 201117Chapter 5

18 Character Pointers and Functions Preferred Code! Spring 201118Chapter 5

19 Pointer Arrays; Pointers to Pointers Spring 201119Chapter 5

20 Multi-dimensional Arrays Spring 201120Chapter 5

21 Multi-dimensional Arrays Write a function that multiplies two n by n square matrices together. Spring 201121Chapter 5

22 Initialization of Pointer Arrays Spring 201122Chapter 5

23 Pointers vs. Multi-dimensional Arrays  a is a true two-dimensional array: 200 int-sized locations have been set aside  The conventional rectangular subscript calculation 20 * row +col is used to find the element a[row,col] Spring 201123Chapter 5

24 Pointers vs. Multi-dimensional Arrays  b is an array of 10 pointers which are not initialized  Assuming that each element of b does point to a twenty- element array, then there will be 200 ints set aside, plus ten cells for the pointers.  The important advantage of the pointer array is that the rows of the array may be of different lengths.  That is, each element of b need not point to a twenty-element vector; some may point to two elements, some to fifty, and some to none at all! Spring 201124Chapter 5

25 Pointers vs. Multi-dimensional Arrays Spring 201125Chapter 5

26 Command-line Arguments Spring 201126Chapter 5

27 Command-line Arguments Spring 201127Chapter 5

28 Exercise TBA Spring 201128Chapter 5


Download ppt "Engineering Computing I Chapter 5 Pointers and Arrays."

Similar presentations


Ads by Google