Pointers and Functions

Pointers and Functions - variable use the address of AP 8...

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

View Full Document Right Arrow Icon
Pointers and Functions Pointer Parameters: Pass by Value formal parameter = actual parameter void show(const char *p) { char *q; printf("[ "); for (q = p; *q != '\0'; q++) printf("%c ", *q); printf("]\n"); }
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: Modifying Actual Parameters (Call by Reference) In order to modify the actual parameter (pass it by reference) use: Trace the execution of: void swap(int *x, int *y) { int temp; temp = *x; *x = *y;
Background image of page 2
Pass by Reference 1. Declare the formal parameter FP as a pointer, for example int *FP 2. In the body of the procedure, dereference FP , that is use *FP 3. In the call if the actual parameter AP , is a
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: variable , use the address of AP ; 8: Modifying Actual Parameters (Call by Reference) • Examples of "Boolean" functions with value/reference parameters: • /* read up to n characters; • * return the number of occurrences of c • */ • int readIt(int n, char c, int 8: Functions Returning Pointers • /* get a block of memory to store int values */ • int* getBlock(size_t size) { • return malloc(size*sizeof(int)); • } • ... • int *p; • if((p = getBlock(10)) == NULL) Pointers • Any memory allocation in a function must be documented clearly • that the client knows who is responsible for freeing this memory....
View Full Document

Page1 / 6

Pointers and Functions - variable use the address of AP 8...

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

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