Passing Pointers

Passing Pointers - int *pi; void f() { int i = 2; 8:...

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

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

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

View Full Document
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;
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))

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

View Full Document
8: The Dangling Reference Problem Stack-based data are deallocated as soon as the function which defines the data terminates:

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

View Full Document

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: 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 = &amp;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

Passing Pointers - int *pi; void f() { int i = 2; 8:...

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

View Full Document
Ask a homework question - tutors are online