Arrays, Functions, and Pointers Flashcards
1
Q
what is the purpose of a pointer compared to a variable?
A
a pointer defaults to the first variable in an array
(ex. int numbers[2] = {10, 20};
int *p = numbers; // p points to the first element of the array)
2
Q
what is the purpose of a function?
A
void changeValue(int *p) {
*p = 99;
}
int x = 10;
changeValue(&x);
printf(“%d”, x); // Prints 99, because we changed x via pointer
3
Q
what is the purpose of an array?
A
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf(“%d “, arr[i]);
}
}
int numbers[3] = {1, 2, 3};
printArray(numbers, 3); // Passes the array to the function