The i-th Object - return (EXIT_FAILURE); } for (i=0;...

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

View Full Document Right Arrow Icon
The i-th Object p[i] is like a regular variable representing the i-th object in a block whose beginning is pointed to by p . In particular, *p is the same as p[0] .
Background image of page 1

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

View Full DocumentRight Arrow Icon
int main () { int *p, i; p = (int *) malloc (5 * sizeof (int)); if (p == NULL) { return (EXIT_FAILURE); } for (i=0; i<5; i++) { *(p+i) = i; } for (i=0; i<5; i++) {
Background image of page 2
int main () { int *p, i; p = (int *) malloc (5 * sizeof (int)); if (p == NULL) { return (EXIT_FAILURE); } for (i=0; i<5; i++) { *(p+i) = i; } for (i=0; i<5; i++) {
Background image of page 3

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

View Full DocumentRight Arrow Icon
int main () { int *p, i; p = (int *) malloc (5 * sizeof (int)); if (p == NULL) { return (EXIT_FAILURE); } for (i=0; i<5; i++) { *(p+i) = i; } for (i=0; i<5; i++) {
Background image of page 4
int main () { int *p, i; p = (int *) malloc (5 * sizeof (int)); if (p == NULL) {
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

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

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

Unformatted text preview: return (EXIT_FAILURE); } for (i=0; i<5; i++) { *(p+i) = i; } printf ("%d ", 0[p]); printf ("%d ", 1[p]); 8: Difference of a Pointer and an Integer Often, we need to access objects preceding the object pointed to by a pointer q . q - n yields a pointer to the p q-2 q-1 q p +2 8: Example #define SIZE 3 double *p, *q; if(MALLOC(p, double, SIZE)) exit(EXIT_FAILURE); /* initialize the block */ /* output in reverse order */ for(i = 0, q = p+SIZE-1; i < SIZE; i++) printf("%f\n", *(q-i)); Memory Given a block of memory of SIZE objects, pointed to by p , you can set q to point to the last object in the block using: p+SIZE-1...
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 / 8

The i-th Object - return (EXIT_FAILURE); } for (i=0;...

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

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