2. What is a pointer?
A type that manipulates aspects of that same type.
A pointer has the following:
Location: actual place where the data is stored in memory
Address: the identifier of the memory location where the data is stored
(i.e. &x == 81300)
Name: what you call the pointer
Value: the address of a variable with the same type.
Ex:
int x = 10; // Copy the value of 10 into the address associated with x
// Assume the address of x, denoted as &x, is 81222
int* pointer; // Declare a pointer that works with int variables
pointer = &x; // pointer is assigned the address of x.
// This is the same as pointer = 81222;
3. Pointer Specification
L-value: an expression that has a location
associated with it. It can appear on the left hand
side or right hand side of an assignment
R-value: an expression that has a value
associated with it but no location associated with
it. It only appears on the right hand side of an
assignment
Ex: int x = 5;
int* a = &x;
*a = 10;
R-value
L-value
4. Pointer Operations
Address Operator(&): a unary operator that only applies to l-
values.
It returns the address (r-value) associated with a variable (l-value)
Ex: int x = 100; printf(“%d”, &x); //This prints an address
Dereference Operator(*): a unary operator that applies to both
l-values and r-values
Applied to an l-value of type T*, it returns an l-value of type T.
Basically, if int* x = &y; then *x == y.
Applied to an r-value of type T, it returns an l-value of type T*.
Ex: int x = 100, y = 20; int* a = &x; int* b = &y;
y = *a; // y = value associated with memory address of x = x
a = *(&b); // a = value associated with memory address of b = b
L-value
R-value
5. In-Class Exercise 1: Pointer Operations
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable: 4996)
int main(){
int x = 5, y = 1, z = 3; // Variables of type
int
int* a, *b, *c; // Pointers to int of type int*
a = &x;
b = &y;
b = a;
printf("%d", *b); // Q1
return 0;
}
Question 1
What value is printed at line Q1?
a) 1
b) 5
c) The address of b
d) The address of a
e) The address of x
f) The address of y
6. In-Class Exercise 1: Pointer Operations
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable: 4996)
int main(){
int x = 5, y = 1, z = 3;
int* a, *b, *c;
a = &x; b = &y; c = &z;
*a = *c;
c = b;
*c = 10;
printf("x = %d y = %dn", x, y);
return 0;
}
Question 2
What value is printed at line Q2?
a) x = 3 y = 10
b) x = 5 y = 1
c) x = 1 y = 3
d) x = 5 y = 10
7. In-Class Exercise 1: Pointer Operations
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable: 4996)
int main(){
int x = 5, y = 1, z = 3;
int* a, *b, *c;
a = &x;
b = &y;
c = &z;
a = *(&b);
b = &(*c);
printf("*a = %d *b = %d *c = %dn", *a, *b, *c); // Q3
return 0;
}
Question 3
What value is printed at line Q3?
a) *a = 1 *b = 3 *c = 3
b) *a = 5 *b = 1 *c = 3
c) *a = 3 *b = 1 *c = 5
d) *a = &b *b = *c *c = 3
8. In-Class Exercise 1: Pointer Operations
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable: 4996)
int main(){
int arr[5] = { 0, 1, 2, 3, 4 };
int *a = &arr[4];
while (*a != 0){
printf("%d ", *a);
a--;
}
printf("n"); // Q4
return 0;
}
Question 4
What value is printed at line Q4?
a) 0 1 2 3 4
b) There is not output
c) Array out of bound exception
d) 4 3 2 1
e) 4 3 2 1 0
9. In-Class Exercise 1: Pointer Operations
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable: 4996)
int main(){
char arr[3][3] = {
{ '0', '1', '2' },
{ '3', '4', '5' },
{ '6', '7', '0' }
};
char* base = &arr[0][0], *a = &arr[0][0];
a = base + 5;
printf("*a = %c ", *a);
a = base + 3;
printf("row = %s", a); // Q5
return 0;
}
Question 5
What is the entire output of the
program after Q5 executes?
a) *a = 5 row = 345670
b) *a = 4 row = 345670
c) *a = 5 row = 34567
d) *a = 6 row = 345670
10. #include <stdio.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
int main() {
int *pointer, i, j, array[6][4];
pointer = malloc(24 * sizeof(int));
for (i = 0; i < 6; i++) {
for (j = 0; j < 4; j++)
*(pointer + (4 * i) + j) = 4 * i + j;
}
printf("%d at %un", *(pointer + 6), pointer + 6);
printf("%d at %un", *(pointer + 7), pointer + 7);
for (i = 0; i < 6; i++) {
for (j = 0; j < 4; j++)
array[i][j] = i * 4 + j;
}
printf("%d at %un", **(array + 5), &array[5][0]);
printf("%d at %un", array[5][1], *(array + 5) + 1);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
20
21
22
23
0Pointer
Pointer + 6
Pointer + 7
.
.
.
In-Class Exercise 2: Pointer Operations
11. Self-Testing
1. If line 7 prints “6 at 12000”, what
will line 8 output?
a) 7 at 12001
b) 7 at 12004
c) 7 at 12008
d) 7 at 12024
2. What kind of variable is at the
address array + 5?
a) A pointer to a pointer to an
integer.
b) A pointer to an integer.
c) An integer.
d) A single array of integers.
3. Why does line 14 add 5 in *(array + 5) + 1 to reach
array[5][1]?
a) The target value is 5 bytes away from array.
b) The target value is 20 bytes away from array, and +5
adds 20 bytes.
c) This code is incorrect, it should be *(array + 20) + 4.
d) This code is incorrect, it should be *(array + 20) + 1.
4. If &array[2] is [9984] and &array[3] is [9992], what is
&array[3] – 2?
a) [9984]
b) [9976]
c) [9990]
d) [9992]
5. In line 13, what is the precedence of the ampersand
(&)?
a) &(array[5][0])
b) (&array[5])[0]
c) (&array)[5][0]
d) None of the above