Chapter 1: Pointers & Structs

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/49

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 5:27 AM on 3/29/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

50 Terms

1
New cards

What is a pointer?

A variable whose value is a memory address.

2
New cards

Declare an int pointer p

int *p

3
New cards

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.

4
New cards

What does the address operator look like?

&

5
New cards

What does this mean:

&x

The memory address of x.

6
New cards

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.

7
New cards

What does the indirection/dereference operator look like? What does it do?

*p

It extracts the value at a memory address.

8
New cards

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

9
New cards

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.

10
New cards

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;

11
New cards

What does this notation mean?

int *p, q;

This is declaring two different variables:

int *p; // an int pointer
int q; // an actual int value

12
New cards

Predict the output of

char c = 'Q';

char *char_pointer = &c;

printf("%c %c \n", c, *char_pointer);

Q Q

13
New cards

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.

14
New cards

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.

15
New cards

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

16
New cards

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.

17
New cards

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=16

Therefore:

b = 11

a = 16

18
New cards

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.

19
New cards

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.

20
New cards

Which line of code would be equivalent to:

(*datePtr).day = 21;

datePtr → day = 21;

21
New cards

What does the → operator do?

It dereferences a pointer and then accesses a member.

22
New cards

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.

23
New cards

Declare a pointer of type struct date

struct date *datePtr;

24
New cards

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

25
New cards

Create a struct with two int pointers: p1 and p2.

Name the struct intPtrs.

struct intPtrs {
int *p1;
int *p2;
};

26
New cards

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 4

pointers 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.

27
New cards

What is this line doing?

pointers.p1 = &i1;
pointers.p2 = &i2;
*pointers.p2 = -97; //this line

This dereferences p2, and sets its value to -97.

28
New cards

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

29
New cards

What does this print?

int a = 6;
int *p =  &a;
int b = *p + 4;
*p = 20;
printf("%d %d \n", a, b);

20, 10

30
New cards

Rewrite this using the arrow operator:

(*ptr).year = 2026;

ptr -> year = 2026;

31
New cards

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;

32
New cards

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.

33
New cards

What will this print?

int a = 4;
int *p = &a;
printf("%d\n", *p);

4

34
New cards

What is the final value of x?

int x = 7;
int *p = &x;
*p = 20;

x has a final value of 20.

35
New cards

What will this print:

int a = 3;
int b = 9;
int *p = &a;
p = &b;
printf("%d\n", *p);

9

36
New cards

What does result hold?

int x = 8;
int *p = &x;
int result = *p + 5 * 2;

result = 18

37
New cards

What will this print?

int a = 10;
int *p = &a;
printf("%d\n", *p + *p);

20

38
New cards

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.

39
New cards

What will this print?

int a = 1;
int *p = &a;
int *q = p;
*p = 5;
*q = *p + 5;
printf("%d\n", a);

10

40
New cards

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 21

41
New cards

What 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 10

42
New cards

What 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 7

43
New cards

Write 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;
}

44
New cards

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;
}

45
New cards

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;
}

46
New cards

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;
}

47
New cards

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;
}

48
New cards

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;
}

49
New cards

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.

50
New cards

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;
}

Explore top notes

note
Music in the Fifteenth Century
Updated 1423d ago
0.0(0)
note
Spanish Final notes
Updated 1023d ago
0.0(0)
note
“OUTLINING”
Updated 1278d ago
0.0(0)
note
Syllabized IGCSE Biology
Updated 214d ago
0.0(0)
note
Music in the Fifteenth Century
Updated 1423d ago
0.0(0)
note
Spanish Final notes
Updated 1023d ago
0.0(0)
note
“OUTLINING”
Updated 1278d ago
0.0(0)
note
Syllabized IGCSE Biology
Updated 214d ago
0.0(0)

Explore top flashcards

flashcards
Waves Unit Terms
24
Updated 197d ago
0.0(0)
flashcards
Leyendas y Mitos
74
Updated 970d ago
0.0(0)
flashcards
(16) reproductive system
71
Updated 1229d ago
0.0(0)
flashcards
Contemporary Visual Arts
54
Updated 194d ago
0.0(0)
flashcards
6/6
53
Updated 298d ago
0.0(0)
flashcards
Latin Roots
115
Updated 1061d ago
0.0(0)
flashcards
Waves Unit Terms
24
Updated 197d ago
0.0(0)
flashcards
Leyendas y Mitos
74
Updated 970d ago
0.0(0)
flashcards
(16) reproductive system
71
Updated 1229d ago
0.0(0)
flashcards
Contemporary Visual Arts
54
Updated 194d ago
0.0(0)
flashcards
6/6
53
Updated 298d ago
0.0(0)
flashcards
Latin Roots
115
Updated 1061d ago
0.0(0)