Pointers and Functions

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

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

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"); }

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

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

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: 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
Ask a homework question - tutors are online