1/4
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
Input/Output (I/O) Parameters in C
These are function parameters implemented using pointers that allow a function to receive an initial value, modify it, and have that change persist in the caller's scope.
Passing variables as I/O parameters
To pass a variable for modification, use the address-of operator && during the function call (e.g., modifyValue(&x);) to provide the memory address of the variable.
Dereferencing in I/O parameters
Inside the function, the dereference operator ∗∗ is used to access and update the actual value stored at the address provided by the pointer (e.g., ∗ptr=100;).
State changes with multiple calls
When a function with I/O parameters is called multiple times on the same variable, each call updates the current state of that variable. For example, if addFive(&n) is called twice, the value of n increases by a total of 1010.
Example of multiple calls to a pointer-based function
Function: void applyStep(int *val) { *val += 1; }
Execution:
int count = 0;
applyStep(&count); (count becomes 11)
applyStep(&count); (count becomes 22)