Pointers
Pointers in C: the address and the & operator, declaration and dereferencing, the null pointer, arithmetic on arrays, passing by address and the dangling pointer.
- 27 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- The numbers
- Scenarios
A few cards from the deck
Memory address: what is it?
Show the answer
A number that locates a cell of memory, as a street number locates a house; in C, it is obtained with the & operator.
A variable occupies one or more cells: &a gives the address of the first. An address is printed with %p, and can change from one run to another.
What does this program print?
```c
int a = 3;
int *p = &a;
*p = 7;
printf("%d\n", a);`- 7
- 3
- 10
- The address of a
Show the answer
7
p holds the address of a: *p = 7 writes to a's cell, so a is 7. Run, the program prints 7. 3 forgets that p designates a; the address would only appear with %p.
For a pointer p of type int *, the expression p + 1 designates the … element: the address increases by … bytes, not by 1.
Show the answer
For a pointer p of type int *, the expression p + 1 designates the next element: the address increases by sizeof(int) bytes, not by 1.
The compiler multiplies by the size of the pointed type. That is why p + 1 lands on the next element of an array, whatever its type.
Pointer: what is it?
Show the answer
A variable whose value is the address of another piece of data; its type states the type of what it designates.
int *p declares a pointer to an int. It takes room itself (8 bytes on a 64-bit system), separate from the room of the data it points to.