L04-AddressingModes - Operands and Addressing Modes Where...

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

View Full Document Right Arrow Icon
L4 – Addressing Modes 1 Comp 411 – Fall 2009 9/9/09 Operands and Addressing Modes Where is the data? Addresses as data Names and Values Indirection Reading: Ch. 2.3
Background image of page 1

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

View Full DocumentRight Arrow Icon
L4 – Addressing Modes 2 Comp 411 – Fall 2009 9/9/09 Just enough C For our purposes C is almost identical to JAVA except: C has “functions”, JAVA has “methods”. function == method without “class”. A global method. C has “pointers” explicitly. JAVA has them but hides them under the covers.
Background image of page 2
L4 – Addressing Modes 3 Comp 411 – Fall 2009 9/9/09 C pointers int i; // simple integer variable int a[10]; // array of integers int *p; // pointer to integer(s) *(expression) is content of address computed by expression . a[k] == *(a+k) a is a constant of type “int *” a[k] = a[k+1] EQUIV *(a+k) = *(a+k+1)
Background image of page 3

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

View Full DocumentRight Arrow Icon
L4 – Addressing Modes 4 Comp 411 – Fall 2009 9/9/09 Legal uses of C Pointers int i; // simple integer variable int a[10]; // array of integers int *p; // pointer to integer(s) p = &i; p = a; p = &a[5]; // address of 6 th element of a *p // value of location pointed by p *p = 1; // change value of that location *(p+1) = 1; // change value of next location p[1] = 1; // exactly the same as above p++; // step pointer to the next element
Background image of page 4
L4 – Addressing Modes 5 Comp 411 – Fall 2009 9/9/09 Legal uses of Pointers int i; // simple integer variable int a[10]; // array of integers int *p; // pointer to integer(s) So what happens when p = &i; What is value of p[0]? What is value of p[1]?
Background image of page 5

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

View Full DocumentRight Arrow Icon
L4 – Addressing Modes 6 Comp 411 – Fall 2009 9/9/09 C Pointers vs. object size Does “p++” really add 1 to the pointer? NO! It adds 4. Why 4? char *q; ... q++; // really does add 1
Background image of page 6
L4 – Addressing Modes 7 Comp 411 – Fall 2009 9/9/09 Clear123 void clear1(int array[], int size) { for(int i=0; i<size; i++) array[i] = 0; } void clear2(int *array, int size) { for(int *p = &array[0]; p < &array[size]; p++) *p = 0; } void clear3(int *array, int size) { int *arrayend = array + size; while(array < arrayend) *array++ = 0; }
Background image of page 7

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

View Full DocumentRight Arrow Icon
L4 – Addressing Modes 8 Comp 411 – Fall 2009
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.

Page1 / 20

L04-AddressingModes - Operands and Addressing Modes Where...

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