Lecture 9 Flashcards
POINTERS
What is a pointer
A pointer is a memory address
What is a pointer variable
A variable that holds a memory address
What does the the compiler need to accesses data via memory address?
the size of the data type and how to interpret the memory bytes
What does the & operatory do?
The & operator gives the address of a variable (or a pointer to a variable)
What is this code doing?
int count;
int *int_pointer = &count;
declaring the int variable count and declaring a pointer to an interger - (initializing the
pointer variable int_pointer to
the address of (or “pointer to”)
count)
What is the last line doing?
int count;
int *int_pointer = &count;
int x = *int_pointer;
It is indirectly accessing count using the * operator - also known as dereferencing
include <stdio.h></stdio.h>
What is the output of this program?
int main (void)
{
char c = ‘Q’;
char *char_pointer = &c;
printf (“%c %c\n”, c, *char_pointer);
c = ‘/’;
printf (“%c %c\n”, c, *char_pointer);
*char_pointer = ‘(‘;
printf (“%c %c\n”, c, *char_pointer);
return 0;
}
Q Q
/ /
( (
include <stdio.h></stdio.h>
What is the output of this program?
int main (void)
{
int i1;
int i2;
int *p1;
int *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);
return 0;
}
i1 = 5, i2 = 12, *p1 = 5, *p2 = 5
What is the equivalence of (*x).y?
x->y
What is the equivalence of
datePtr = &today;
(datePtr).day = 5;
(datePtr).month = 10;
(*datePtr).year = 2023;
datePtr = &today;
datePtr->month = 10;
datePtr->day = 5;
datePtr->year = 2023;
include <stdio.h></stdio.h>
What is the ouput of this program?
struct intPtrs
{
int *p1;
int *p2;
};
int main (void)
{
struct intPtrs pointers;
int i1 = 100
int 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);
return 0;
}
i1 = 100, *pointers.p1 = 100
i2 = -97, *pointers.p2 = -97
What is a practical example that showcases the use of pointers
Linked-list or “node chain”