Pointer Arithmetic

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

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

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.

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

View Full Document
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 .
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

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

View Full Document
8: The Sum of a Pointer and an Integer: i-th object To access

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

View Full Document
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
Ask a homework question - tutors are online