Passing Pointers

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

Info iconThis 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
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: 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;
Background 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))
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 Dangling Reference Problem Stack-based data are deallocated as soon as the function which defines the data terminates:
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

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: 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

Page1 / 8

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 Right Arrow Icon
Ask a homework question - tutors are online