1/49
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What is a pointer?
A variable whose value is a memory address.
Declare an int pointer p
int *p
What does p currently store?
int *p
p stores the address of an int.
Since it hasn’t been assigned a valid address yet, it currently stores garbage.
What does the address operator look like?
&
What does this mean:
&x
The memory address of x.
What does each variable contain?
int count = 10;
int *int_pointer;
int_pointer = &count;count holds an int value.
int_pointer holds the address of count.
What does the indirection/dereference operator look like? What does it do?
*pIt extracts the value at a memory address.
If the dereference operator uses the same * symbol as a pointer, how do we know when something is being dereferenced vs when a new pointer is being created?
Based on context.
If something is declared as int *p, it is referring to an int pointer.
But otherwise, outside of this context, it is typically referring to the dereference operator: int x = *int_pointer
What does x contain?
int x;
int *int_pointer;
int_pointer = &count;
x = *int_pointer;x contains the value at the address stored by int_pointer.
What does this notation mean?
int count = 10, x;This is declaring two different variables at once, and only initializing one of them. It’s the same as saying:
int count = 10;
int x;What does this notation mean?
int *p, q;This is declaring two different variables:
int *p; // an int pointer
int q; // an actual int valuePredict the output of
char c = 'Q';
char *char_pointer = &c;
printf("%c %c \n", c, *char_pointer);Q Q
What about the output of:
c = '\';
*char_pointer = &c;
printf("%c %c\n", c, *char_pointer);\ \
This is because we dereferenced char_pointer and extracted its value, giving us the same result.
Predict the output of:
c = '\';
*char_pointer = '(';
printf("%c %c \n", c, *char_pointer);assuming that char_pointer holds the address of c.
( (
This is because we dereferenced char_pointer, assigning the value of ( to c.
Predict the output of:
int i1, i2;
int *p1, *p2;
i1 = 5;
p1 = &i1;
i2 = *p1/2+10;
p2 = p1;
printf("i1 = %i, i2 = %i, *p1 = %i, *p2 = %i\n", i1, i2, *p1, *p2);5, 12, 5, 5
True or False:
If p is a pointer to an integer, than *p is an integer expression
True. The moment you add the dereference operator, it becomes an expression.
What value does a and b store after the code runs?
int a = 8;
int *p = &a;
int b = *p + 3;
*p = *p + 2;int a = 8; // holds 8
int *p = &a; // holds the address of a
int b = *p + 3; // holds 8+3=11
*p = *p * 2; // the value held by the address at p is 8*2=16Therefore:
b = 11
a = 16
What’s wrong with the following code:
int a = 8;
int *p = &a;
int b = *p+3;
p = *p*2;The problem is in the last line: p = *p*2;
Since p is not dereferenced, you are trying to store an int value inside of an int * (pointer) variable.
This is a type mismatch, and is therefore not allowed.
Given the following, what does each variable contain?
struct date {
int month;
int day;
int year;
};
struct date today;
struct date *datePtr;
datePtr = &today;
(*datePtr).day = 21;today contains one entire struct with three different fields: a month, day, and year.
datePtr contains the memory address of the variable today. This means that (*datePtr).day sets the value of day in the “today” variable to 21. By using a pointer, we’re accessing and setting the value of that variable directly.
Which line of code would be equivalent to:
(*datePtr).day = 21;datePtr → day = 21;
What does the → operator do?
It dereferences a pointer and then accesses a member.
Why do we have to include brackets when we’re dealing with the dereference operator and dot operator?
(*datePtr).day = 21;We have to use parentheses because the dot operator . has a higher precedence than *. Doing it without the brackets would result in incorrect parsing.
Declare a pointer of type struct date
struct date *datePtr;Predict the output of:
struct date today, *datePtr;
datePtr = &today;
datePtr -> month = 9;
datePtr -> day = 25;
datePtr -> year = 2025;
printf("Today's date is: %i/%i/%.2i/n", datePtr -> month, datePtr -> day, datePtr -> year % 100);9/25/25
Create a struct with two int pointers: p1 and p2.
Name the struct intPtrs.
struct intPtrs {
int *p1;
int *p2;
};What are lines 3 and 4 doing here?
struct intPtrs {
int *p1;
int *p2;
};
struct intPtrs pointers;
int i1 = 100, i2;
pointers.p1 = &i1; // line 3
pointers.p2 = &i2; // line 4pointers contains one entire intPtrs struct.
pointers.p1 = &i1; is using the dot operator to set the value of p1 to &i1 (the address of i1). Since we are assigning an address, not an int value, we do not need to dereference.
What is this line doing?
pointers.p1 = &i1;
pointers.p2 = &i2;
*pointers.p2 = -97; //this lineThis dereferences p2, and sets its value to -97.
What does this print?
int i1 = 100, i2;
pointers.p1 = &i1;
pointers.p2 = &i2;
*pointers.p2 = -97;
printf("i1 = %i, *pointers.p1 = %i\n", i1, *pointers.p1);
printf("i2 = %i, *pointers.p2 = %i\n", i2, *pointers.p2);Exact Output:
i1 = 100, *pointers.p1 = 100
i2 = -97, *pointers.p2 = -97
What does this print?
int a = 6;
int *p = &a;
int b = *p + 4;
*p = 20;
printf("%d %d \n", a, b);20, 10
Rewrite this using the arrow operator:
(*ptr).year = 2026;ptr -> year = 2026;Write code so that:
p.x points to a
p.y points to b
then change b to 100 using only the pointer inside the structure.
struct pair {
int *x;
int *y;
};
struct pair p;
int a = 3, b = 7;p.x = &a;
p.y = &b;
*p.y = 100;Describe the differences between:
*p.y = 100;
(*p).y = 100;
p -> y = 100;*p.y = 100;This is almost always wrong , it’s equivalent to *(p.y) = 100. It means that p is a struct, not a pointer, and that p.y is a pointer to an int value, otherwise, it would be wrong.
(*p).y = 100;p -> y = 100;These two lines are identical in meaning; It means: get the value of y from the pointer to a struct p, and set it to 100.
What will this print?
int a = 4;
int *p = &a;
printf("%d\n", *p);4
What is the final value of x?
int x = 7;
int *p = &x;
*p = 20;x has a final value of 20.
What will this print:
int a = 3;
int b = 9;
int *p = &a;
p = &b;
printf("%d\n", *p);9
What does result hold?
int x = 8;
int *p = &x;
int result = *p + 5 * 2;result = 18
What will this print?
int a = 10;
int *p = &a;
printf("%d\n", *p + *p);20
Which of these is valid?
struct S {
int *x;
};
struct S s;
int a = 4;
s.x = &a;Answer:
*s.x = 20;
This is because s.x is the name of the pointer. s by itself is just a struct variable.
What will this print?
int a = 1;
int *p = &a;
int *q = p;
*p = 5;
*q = *p + 5;
printf("%d\n", a);10
What will this print?
struct data {
int value;
};
struct data d = {7};
struct data *p = &d;
struct data *q = p;
q->value = p->value * 3;
printf("%d\n", d.value);21
Breakdown:
struct data {
int value;
};
struct data d = {7}; // a struct variable, initializes value to 7
struct data *p = &d; // points to a struct
struct data *q = p; // points to the same struct
q->value = p->value * 3; // the value of d = 21
printf("%d\n", d.value); // prints 21What will this print?
struct wrap {
int *ptr;
};
int x = 4;
int y = 10;
struct wrap w1, w2;
w1.ptr = &x;
w2.ptr = &y;
*w1.ptr = *w2.ptr + 1;
printf("%d %d\n", x, y);11 10
Breakdown:
struct wrap {
int *ptr;
};
int x = 4;
int y = 10;
struct wrap w1, w2; // creates 2 variables, each containing one entire struct
w1.ptr = &x; // holds the address of x
w2.ptr = &y; // holds the address of y
*w1.ptr = *w2.ptr + 1; // the value at w1 = 10 + 1
printf("%d %d\n", x, y); // prints 11 10What will this print?
int a=3, b=6, c=9;
int *p = &a;
int *q = &b;
*p = *q;
q = &c;
*q = *p+1;
printf("%d %d %d\n", a, b, c);6 6 7
Breakdown:
int a=3, b=6, c=9;
int *p = &a; // holds the address of a
int *q = &b; // holds the address of b
*p = *q; // the value at p = 6
q = &c; // q = the address of c
*q = *p+1; // the value of q = 6+1
printf("%d %d %d\n", a, b, c);
// prints 6 6 7Write a program that declares an int variable with a value of 12, declares a pointer p that points to a, and computes a new integer b using the expression: b = (*p+8)/2-*p
Print the value of a, *p, and b.
#include <stdio.h>
int main(void) {
int a = 12;
int b;
int *p = a;
b = (*p+8)/2-*p;
printf("%d, "%d", a, *p, b);
return 0;
}Write a program that declares an integer x=5, declares two pointers p1 and p2, makes both pointers point to x. Use p1 to multiply x by 3, use p2 to subtract 4 from x. Print the final value of x, *p1, and *p2
#include <stdio.h>
int main(void) {
int x = 5;
int *p1 = &x;
int *p2 = &x;
*p1 = *p1 * 3;
*p2 = *p2 - 4;
printf("%d %d %d", x, *p1, *p2);
return 0;
}Define the struct: struct point { int x; int y; };
Write a program that creates a struct point pt with values 2, 3. Then create a pointer that points to pt, use the pointer to set x to 10, y to ptr→x+5, then print the final values of pt.x and pt.y. Use →
#include <stdio.h>
int main(void) {
struct point {
int x;
int y;
};
struct point pt;
pt.x = 2;
pt.y = 3;
struct point *p = &pt;
p->x = 10; // x = 10
p->y = ptr->x+5; // y = 15
printf("%d %d", pt.x, pt.y);
return 0;
}Define the struct
struct pair {
int *a;
int *b;
};
Write a program that declares two ints, m=7 and n=14. Declare a variable p of type struct pair. Make p.a point to m, make p.b point to n. Use this to set m to *p.b-2 and set n to *p.a+10. Print m and n.
#include <stdio.h>
int main(void) {
struct pair {
int *a;
int *b;
};
int m;
int n;
m = 7;
n = 14;
struct pair p;
p.a = &m;
p.b = &n;
*p.a = *p.b-2;
*p.b = *p.a+10;
printf("%d %d", m, n);
return 0;
}Define
struct point {
int x;
int y;
};
Create a struct point p={4,9}. Create a pointer ptr to p. Use the pointer to double the value of x, set y equal to ptr→x-3. Print the values of p.x and p.y.
#include <stdio.h>
int main(void) {
struct point {
int x;
int y;
};
struct point p = {4,9};
struct point *ptr = &p;
(*ptr).x = (*ptr).x * 2;
ptr->y = ptr->x-3;
printf("%d %d", p.x, p.y);
return 0;
}Define
struct pair {
int *a;
int *b;
};
Write a program that declares three integers x=2, y=7, z=20. Declare a pointer variable p of type struct pair. Make p.a point to x and p.b point to y. Set x to *p.b+1, reassign p.b so it points to z, set z to *p.a + *p.b Print the values of x, y, and z. Do not directly assign to x, y and z after initialization.
#include <stdio.h>
int main(void) {
struct pair {
int *a;
int *b;
};
int x = 2;
int y = 7;
int z = 20;
struct pair p; // we do NOT declare p as a pointer because the struct already contains pointers
p.a = &x;
p.b = &y;
*p.a = *p.b+1;
p.b = &z;
*p.b = *p.a + *p.b;
printf("%d %d %d", x, y, z);
return 0;
}Does the → work with any kind of pointer?
No! The → operator only works when the thing on the left is a pointer to a struct.
This is why in the previous question, (p.a)→ would have been invalid. Because p was not a pointer to a struct, and p.a was only a pointer to an int variable, not to a struct.
Write a program that declares three integers: a = 5, b = 12, c = 30. Declare 3 pointers: p, q, and r. Make p point to a, q point to b, r point to c. Set the value pointed to by p equal to the value pointed to by q minus 2. Reassign q so that it points to a.
Set the value pointed to by r equal to the value pointed to by q plus the value pointed to by p. Reassign p so that it points to c. Set the value pointed to by p equal to the value pointed to by r minus 100. Print a, b, and c.
#include<stdio.h>
int main(void) {
int a = 5;
int b = 12;
int c = 30;
int *p, *q, *r;
p = &a;
q = &b;
r = &c;
*p = *q-2;
q = &a;
*r = *q + *p;
p = &c;
*p = *r-100;
printf("%d %d %d", a, b, c);
return 0;
}