cse240 pointers
TRANSCRIPT
![Page 1: CSE240 Pointers](https://reader036.vdocuments.site/reader036/viewer/2022083114/58edc4371a28ab50158b4727/html5/thumbnails/1.jpg)
CSE240 Pointers
![Page 2: CSE240 Pointers](https://reader036.vdocuments.site/reader036/viewer/2022083114/58edc4371a28ab50158b4727/html5/thumbnails/2.jpg)
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 81222int* pointer; // Declare a pointer that works with int variablespointer = &x; // pointer is assigned the address of x. // This is the same as pointer = 81222;
![Page 3: CSE240 Pointers](https://reader036.vdocuments.site/reader036/viewer/2022083114/58edc4371a28ab50158b4727/html5/thumbnails/3.jpg)
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
![Page 4: CSE240 Pointers](https://reader036.vdocuments.site/reader036/viewer/2022083114/58edc4371a28ab50158b4727/html5/thumbnails/4.jpg)
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-valueR-value
![Page 5: CSE240 Pointers](https://reader036.vdocuments.site/reader036/viewer/2022083114/58edc4371a28ab50158b4727/html5/thumbnails/5.jpg)
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 intint* a, *b, *c; // Pointers to int of type int*
a = &x;b = &y;b = a;printf("%d", *b); // Q1
return 0;}
Question 1What value is printed at line Q1?a) 1b) 5c) The address of bd) The address of ae) The address of xf) The address of y
![Page 6: CSE240 Pointers](https://reader036.vdocuments.site/reader036/viewer/2022083114/58edc4371a28ab50158b4727/html5/thumbnails/6.jpg)
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 = %d\n", x, y);
return 0;}
Question 2What value is printed at line Q2?a) x = 3 y = 10b) x = 5 y = 1c) x = 1 y = 3d) x = 5 y = 10
![Page 7: CSE240 Pointers](https://reader036.vdocuments.site/reader036/viewer/2022083114/58edc4371a28ab50158b4727/html5/thumbnails/7.jpg)
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 = %d\n", *a, *b, *c); // Q3return 0;}
Question 3What value is printed at line Q3?a) *a = 1 *b = 3 *c = 3b) *a = 5 *b = 1 *c = 3c) *a = 3 *b = 1 *c = 5d) *a = &b *b = *c *c = 3
![Page 8: CSE240 Pointers](https://reader036.vdocuments.site/reader036/viewer/2022083114/58edc4371a28ab50158b4727/html5/thumbnails/8.jpg)
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 4What value is printed at line Q4?a) 0 1 2 3 4b) There is not outputc) Array out of bound
exceptiond) 4 3 2 1e) 4 3 2 1 0
![Page 9: CSE240 Pointers](https://reader036.vdocuments.site/reader036/viewer/2022083114/58edc4371a28ab50158b4727/html5/thumbnails/9.jpg)
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); // Q5return 0;
}
Question 5What is the entire output of the program after Q5 executes?a) *a = 5 row = 34567\0b) *a = 4 row = 34567\0c) *a = 5 row = 34567d) *a = 6 row = 34567\0
![Page 10: CSE240 Pointers](https://reader036.vdocuments.site/reader036/viewer/2022083114/58edc4371a28ab50158b4727/html5/thumbnails/10.jpg)
#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 %u\n", *(pointer + 6), pointer + 6);printf("%d at %u\n", *(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 %u\n", **(array + 5), &array[5][0]);printf("%d at %u\n", array[5][1], *(array + 5) + 1);return 0;
}
123456789101112131415
123456789101112
20212223
0Pointer
Pointer + 6Pointer + 7
...
In-Class Exercise 2: Pointer Operations
![Page 11: CSE240 Pointers](https://reader036.vdocuments.site/reader036/viewer/2022083114/58edc4371a28ab50158b4727/html5/thumbnails/11.jpg)
Self-Testing1. If line 7 prints “6 at 12000”, what will line 8 output?a) 7 at 12001b) 7 at 12004c) 7 at 12008d) 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