{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Passing Pointers

Passing Pointers - • int*pi • void f • int i = 2 •...

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

View Full Document Right Arrow Icon
Passing Pointers by References int getBlockRef(int **p, unsigned n) { if((*p = (int*)malloc(n*sizeof(int))) == NULL) return 0; return 1; } ... "Pass by Reference" Idiom
Image of page 1

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

View Full Document Right Arrow Icon
8: Protecting Return Values and Parameter Values: const double product(double *block, int size); /* Does it modify memory pointed to by block?*/ double product(double *block, int size) { int i; for (i = 1; i < size; i++)
Image of page 2
8: Protecting Return Values and Parameter Values: const double product(const double *block, int size); /* return a pointer to a constant */ const int* f(int p) { int *i; if((i = malloc(sizeof(int)) == NULL)
Image of page 3

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

View Full Document Right Arrow Icon
8: The Dangling Reference Problem Stack-based data are deallocated as soon as the function which defines the data terminates:
Image of page 4
Image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: • int *pi; • void f() { • int i = 2; • 8: Pointers to Functions • A pointer to a function determines the prototype of this function, • but it does not specify its implementation: • int (*fp)(double); /* a pointer to a function */ • int *fp(double); /* a function returning . .. */ • You can assign an existing function to the pointer as long as both have Errors: Generic Pointers • int i; • void *p = &i; • *p = 2; • *(int*)p = 2; Errors: Generic Pointers • void* f() { • int *ip; • if((ip = (int*)malloc(sizeof(int))) == NULL) • error ; • return ip; 8: Pointers to Blocks Containing Pointers block 2.1 3.1 4.1...
View Full Document

{[ snackBarMessage ]}