[Week 11] Intermediate Programming 2
[Week 11] Intermediate Programming 2
Intermediate
Programming 2
Pointers
- Two different usages of '*'
int* pointer type
*p dereference operator (value-of operator)
- Address-of operator '&'
Pointer Usages
- We must initialize pointers before using them.
- Without pointers initializations, we will have unaware accesses to garbage addresses.
Pointers
as Return Values
Pointers as a Return Type
int max_value, i, j;
…
After the call, max points to either num1 or num2. max_value = max(i, j);
num1
max 10
20
num2
Pointers as a Return Type DEMO
int main() {
#include <stdio.h>
srand(0);
#include <stdlib.h>
int num1 = rand() % 100, num2 = rand() % 100;
printf("num1: %d\n", num1);
int max(int a, int b) {
printf("num2: %d\n", num2);
if (a > b)
printf("max: %d\n\n", max(num1, num2));
return a;
else
int *max = max_pointer(&num1, &num2);
return b;
int max_value = *max;
}
*max = 0;
int* max_pointer(int *a, int *b) {
printf("num1: %d\n", num1);
if (*a > *b)
printf("num2: %d\n", num2);
return a;
printf("max: %d\n", *max);
else
printf("max: %d\n", max_value);
return b;
return 0;
}
}
Pointers for Multiple Returns
int main()
{
int num1, num2, num3;
scanf("%d %d %d", &num1, &num2, &num3);
printf("%d %d %d", num1, num2, num3);
}
scanf can be called with multiple variables and the results are
stored in each variable.
- You can understand it as returning multiple values from a function call.
Exercise: min_max function DEMO
Make a function that returns the min and max value of an array.
- You can pass pointers to functions to store the return values.
<Source Code>
}
Exercise: min_max function (Solution) DEMO
Make a function that returns the min and max value of an array.
- You can pass pointers to functions to store the return values.
Make a function that returns the min and max value of an array.
- You can pass pointers to functions to store the return values.
int main()
{
int num[10];
init_array(num, 10); // a function for initializing an array
print_array(num, 10); // a function for printing an array
int min, max;
min_max(num, 10, &min, &max);
printf("min: %d, max: %d\n", min, max);
}
.
BASIS AND PRACTICE IN PROGRAMMING
Intermediate
Programming 2
More examples:
The meaning of "+" with a pointer is moving the pointer to a "next element".
The meaning of "-" with a pointer is moving the pointer to a "previous element".
Pointer Arithmetic with Graphical Explanation
'+' example
'-' example
#define N 10
…
int a[N]={1,2,3,4,5,6,7,8,9,10}, sum, *p;
…
sum = 0;
for (p = &a[0]; p < &a[N]; p++)
sum += *p;
Relationship between Pointers and Arrays
The name of an array can be used as a pointer to the first element in the array.
- An array name can be thought of as a constant pointer.
- We can set aPtr equal to the address of the first element in array a as above code.
- '3' in the 4th line is an offset, and the value is identical to the array index.
Note that 'aPtr + 3' is equivalent to '&a[3]' when the pointer points to the array's first element.
- This notation is referred to as pointer/offset notation.
Using an Array Name as a Pointer
The name of an array can be used as a pointer to the first element in the array.
- An array name can be thought of as a constant pointer.
The addition and subtraction of pointers would easily go outside the valid
memory range.
- Compilers do not check the valid boundary.
- It will cause a memory-related error.
#define N 10
…
int a[N]={1,2,3,4,5,6,7,8,9,10}, sum, *p;
…
sum = 0;
for (p = a; p < a+N; p++)
sum += *p;
Using an Array Name as a Pointer Examples
Original version:
for (p = &a[0]; p < &a[N]; p++)
sum += *p;
Simplified version 1:
for (p = a; p < a + N; p++)
sum += *p;
Simplified version 2:
p = a;
while (p < a+N)
p++;
Exercise: min_max function (Pointer Arithmetic) DEMO
Make a function that returns the min and max value of an array.
- You must use pointer arithmetic while traversing the array.
<Source Code>
}
Exercise: min_max function (Pointer Arithmetic, Solution) DEMO
Make a function that returns the min and max value of an array.
- You must use pointer arithmetic while traversing the array.
Make a function that returns the min and max value of an array.
- You must use pointer arithmetic while traversing the array.
int main()
{
int num[10];
init_array(num, 10); // a function for initializing an array
print_array(num, 10); // a function for printing an array
int min, max;
min_max(num, 10, &min, &max);
printf("min: %d, max: %d\n", min, max);
}
Reading Materials
Pointer Arithmetic
- When an integer is added to or subtracted from a pointer, the pointer is not incremented or
decremented simply by that integer, but by that integer times the size of the object to which the
pointer refers. The number of bytes depends on the object’s data type.
- If an integer is stored in 2 bytes of memory, then the preceding calculation would result in
memory location 3004 (3000 + 2 * 2).
- When performing pointer arithmetic on a character array, the results will be consistent with
regular arithmetic, because each character is 1 byte long.
.
BASIS AND PRACTICE IN PROGRAMMING
Intermediate
Programming 2