Pointer Arithmetic - the i-th object in a memory block:...

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

View Full Document Right Arrow Icon
8: Pointer Arithmetic Valid operations on pointers include: - the sum of a pointer and an integer - the difference of a pointer and an integer - pointer comparison - the difference of two pointers.
Background image of page 1

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

View Full DocumentRight Arrow Icon
8: The Sum of a Pointer and an Integer To access other objects in the block of memory pointed to p , use p + n where n is an integer. This expression yields a pointer to the n -th object beyond the one that p currently points to. The exact meaning of "object" is determined by the type of p .
Background image of page 2
8: The Sum of a Pointer and an Integer #define SIZE 3 double *p; if((p = malloc(SIZE*sizeof(double))) == NULL) exit(EXIT_FAILURE); p p+1 p +2
Background image of page 3

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

View Full DocumentRight Arrow Icon
8: The Sum of a Pointer and an Integer: i-th object To access
Background image of page 4
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: the i-th object in a memory block: *(p+i) or p[i] 8: The Sum of a Pointer and an Integer: i-th object #define SIZE 3 double *p; if((p = malloc(SIZE*sizeof(double))) == NULL) exit(EXIT_FAILURE); for(i = 0; i < SIZE; i++) 8: The Sum of a Pointer and an Integer: i-th object &p[i] &*(p+i) p+i are equivalent. /* read in using */ for(i = 0; i < SIZE; i++) if(scanf("%lf", &p[i]) == 0) exit(EXIT_FAILURE); 8: The Sum of a Pointer and an Integer: Traversals Pointers are often used for traversals in loops, for example double *pi; for(i = 0, pi = p; i < SIZE; i++, pi++) product *= *pi;...
View Full Document

This note was uploaded on 02/18/2012 for the course CS 240 taught by Professor Rego during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 7

Pointer Arithmetic - the i-th object in a memory block:...

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

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