A pointer in C is a variable that stores…
A pointer holds an address. In int *p = &x;, p contains the address of x, and *p reaches the value stored there.
Pointers · Exercise 1 of 4
Twelve multiple-choice questions on what pointers are, the & and * operators, NULL, void and const pointers. Pick an option to check your answer instantly.
A pointer in C is a variable that stores…
A pointer holds an address. In int *p = &x;, p contains the address of x, and *p reaches the value stored there.
Which operator gives the address of a variable?
& is the address-of operator: &x is the address of x. * does the opposite – it follows an address to the value (dereference).
If p holds the address of x, which expression gives the value of x?
*p dereferences p: it goes to the address p holds and reads the int there, which is x. p is the address itself and &p is the address of the pointer variable.
After the declaration int *p, q; what are p and q?
The * belongs to the name next to it, not to the type. To declare two pointers write int *p, *q;. This is why many style guides declare one pointer per line.
On a typical 64-bit system, what is sizeof(char *) compared with sizeof(double *)?
A pointer stores an address, and every address on a given system has the same size – 8 bytes on 64-bit, 4 bytes on 32-bit. The pointed-to type changes how far p + 1 moves, not the size of p.
Which statement about a NULL pointer is true?
NULL means "points to nothing". You can compare against it (if (p == NULL)), but reading or writing *p through it is undefined behaviour – usually a crash. An uninitialised pointer is different: it holds a random address you cannot test for.
int *p holds the address 1000 and an int is 4 bytes. What address does p + 1 hold?
Pointer arithmetic is scaled by the size of the pointed-to type: p + 1 = 1000 + 1 × sizeof(int) = 1004. For a char * it would be 1001, for a double * 1008.
For an array a, the expression a[i] is equivalent to…
The C standard defines a[i] as *(a + i): move i elements from the start, then dereference. *a + i adds i to the first element's value instead.
Which is true about void *vp?
A void * is a generic pointer – malloc() returns one. Because the compiler does not know the type at that address, you must convert it, e.g. *(int *) vp, before dereferencing.
A dangling pointer is a pointer that…
After free(p), or after a function returns the address of its local variable, the pointer still holds the old address but the memory is no longer valid. Setting p = NULL after freeing avoids it. A pointer that was never initialised is called a wild pointer.
Timed online tests with instant scores and explanations.