What is Pointer?

A variable whose value is the memory address of a variable, an array element, a function or other object. In C it is declared as in int *p; &x yields the address of x, and *p reads or writes the object p points to. Adding an integer to a pointer advances it in units of the size of the pointed-to type. In most expressions an array name is converted to a pointer to its first element, so a[i] means the same as *(a + i). A pointer value that points nowhere is called a null pointer.

Japanese name: ポインタ

Pointer appears in 26 practice questions across 8 topics, including C Programming Basics, Data Structures and Algorithms, ISA & Assembly.

Field: C Programming Basics

Declaration and basic operations

int *p; declares p as a "pointer to int". After p = &x; stores the address of the variable x, *p = 5; writes 5 into x. Function arguments are passed by value, so to change a caller's variable inside a function, its address is passed as a pointer. To use a member through a pointer to a struct, (*p).m is written as p->m.

Pointer arithmetic and arrays

Adding 1 to a pointer to int advances the address not by one byte but by sizeof(int) bytes (4 bytes in many environments). An array name is converted to a pointer to its first element, except in cases such as when it is the operand of sizeof or &. So when an array is passed to a function, only the address of its first element is actually passed, and sizeof inside the function cannot tell the size of the whole array.

Use in embedded code and pitfalls

On a microcontroller, peripheral registers are read and written by converting an integer address into a pointer, as in (volatile uint32_t *)0x40020014. Reading or writing through an uninitialized pointer, a null pointer or a dangling pointer to freed memory is undefined behavior, and without an OS it can silently corrupt other data.

Try it: questions about Pointer

Compared with an array, which is a correct description of a linked list (a list in which each element holds a pointer to the next element)?

  1. AIf the element just before the insertion point is known, a new element can be inserted just by changing pointers without moving other elements, but reading the i-th element requires following the list from the front
  2. BThe position of the i-th element can be computed, so any element can be read in constant time, O(1)
  3. CThe elements are stored contiguously in memory, so caches and prefetching work well when reading them in order
Show answer and explanation

Answer: A. If the element just before the insertion point is known, a new element can be inserted just by changing pointers without moving other elements, but reading the i-th element requires following the list from the front

In a linked list, once the position is known, insertion and deletion only require rewriting the neighboring pointers, which takes O(1). Reading the i-th element, however, requires following i pointers from the front, which takes O(n). An array can compute the address of the i-th element from its start address and element size, so it reads any element in O(1), but inserting in the middle takes O(n) to shift the following elements. Also, because array elements are contiguous, caches and prefetching work well, whereas the nodes of a linked list tend to be scattered in memory, so even an O(n) traversal can be much slower.

int is 4 bytes and pointers are 8 bytes. When the value (address) of a pointer to int p is 0x1000, which of the following is the value of p + 3?

  1. A0x100C
  2. B0x1012
  3. C0x1018
Show answer and explanation

Answer: A. 0x100C

Adding an integer to a pointer advances it in units of the size of the pointed-to type. int is 4 bytes, so p + 3 points 3 × 4 = 12 bytes ahead, that is, to 0x1000 + 0xC = 0x100C. p + 3 is the address of the array element p[3], and *(p + 3) means the same as p[3]. A pointer to char advances 1 byte at a time, and a pointer to double (8 bytes) 8 bytes at a time.

FAQ

How large is a pointer?

In many 64-bit environments it is 8 bytes, and on 32-bit microcontrollers 4 bytes. Data pointers generally have the same size whether they point to char or to double. A pointer is not necessarily the same size as an int, so uintptr_t from stdint.h is used when an address must be stored in an integer.

What is the difference between const char *p and char *const p?

const char *p is a "pointer to const char": the characters cannot be modified through p, but p itself can be changed to point elsewhere. char *const p is a "const pointer": p cannot be changed, but what it points to can be modified. Read it as: const to the left of * fixes what is pointed to, and const to the right fixes the pointer itself.

Pointer in the explanations

Questions that mention Pointer (26)

  1. Compared with an array, which is a correct description of a linked list (a list in which each element holds a pointer to the next element)?
  2. int is 4 bytes and pointers are 8 bytes. When the value (address) of a pointer to int p is 0x1000, which of the following is the value of p + 3?
  3. int is 4 bytes and pointers are 8 bytes. Which of the following is the output when the program above is executed?
  4. Which of the following is true about the pointer returned by the function make above?
  5. In C, which of the following describes what happens when a null pointer is dereferenced (*p is read while p is NULL)?
  6. In the standard RISC-V calling convention, when calling a function with ten integer arguments, where are the ninth and tenth passed?
  7. Which correctly describes an ABI (application binary interface)?
  8. How is a call through a C function pointer, "(*fp)(x)", generally performed in RISC-V?
  9. In the function above, why can the compiler not simply hoist the load of *b out of the loop?
  10. Which kind of instruction does a compiler's automatic vectorization use?
  11. What generally happens when a program accesses an address the OS has not allocated?
  12. Which describes the ABA problem that affects lock-free data structures built with CAS?
  13. Which describes CUDA unified memory?
  14. In CUDA, how does the CPU-side program receive the results computed by a GPU kernel (a function marked __global__)?
  15. A binary heap is stored in an array a[0], a[1], a[2], … with the root at a[0], filling each level from left to right starting at the top. Which are the children and the parent of a[4]?

Books on C Programming Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Array Variable Undefined behavior Argument Stack Linked list Prefetching Dynamic memory allocation Page fault Microcontroller (MCU)

Back to the glossary