Lecture 4 Flashcards

Pointers

1
Q

datatype* name;

A

pointer initialization, “*” after data type –> creates a pointer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

&name

A

&: returns the memory address of where the variable is stored

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

int i = 10;
int* p = &i;

what is p assigned to?

A

int pointer, p, holds the address location of another variable (int i) stored in memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

int i = 10;
int* p = &i;
printf(“%p\n”, p);
:
:
i –> memory address is 1056.

what does this print?

A

prints the address in main memory that this pointers, p, pointing to
prints 1056 –> int i memory address that pointer p points to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

%p

A

format specifier that expects pointers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

pointer on 64-bit system

A

= 8 bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

int on 64-bit system

A

= 4 bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how many address locations do you need for an int on a 64-bit system?

A

4 address locations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

pointer dereferencer

A

*NameOfPointer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what does *nameOfPointer do?

A
  • : dereferences the pointer from the memory address of the variable, and sets it to the value stored at that memory address
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

int i = 10;
int * p = &i;
printf(“%d\n”, *p);
:
:
Main Mem Addy
i 10 1056
:
p 1056 2000
what does this print?

A

dereferences pointer p @ 1056 (memory address of int i), and prints the value stored there instead
prints 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

int i = 10;
int* p = &i;
*p = 20;
printf(“%d\n”, *p);
:
:
Main Mem Addy
i 20 1056
:
p 1056 2000

what does this print?

A

dereferences the pointer p @ 1056 and changes the value to 20 at that same memory address (*p = 20)
prints 20 (int i = 20 now)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

int i = 10;
int* p = &i;

what line of code do we need if we want to print the value p points to?

A

printf(“%d\n”, *p);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

int i = 10;
int* p = &i;
int* q = p;
printf(“%d\n”, *q);
:
:
Main Mem Addy
i 10 1056
:
p 1056 2000
q 1056 2008

what does this “int* q = p;” mean?

A

int pointer q points to pointer p which points to the memory address of int i. q points to the memory address of i since p points there and q points to p.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

int i = 10;
int* p = &i;
int* q = p;
printf(“%d\n”, *q);
:
:
Main Mem Addy
i 10 1056
:
p 1056 2000
q 1056 2008

what does gets printed?

A

prints 10
q points @ p
p points @ 1056
q points @ 1056
dereferenced q (*q) –> prints 10 because that’s the value stored at that memory address (1056)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Main Mem Addy
i 10 1056
:
p 1056 2000
q 1056 2008

what are 3 different ways to get the value stored at 1056?

A
  1. print i –> set to 10 and assigned to the memory address, 1056
  2. use *p –> dereferenced p, gets the value stored at the memory address, 1056, that it points to
  3. use *q–> q points to p, so it points to 1056, dereferenced q gets the value stored at the memory address, 1056, since it points to the same address as p
17
Q
A