Pointers in C

Every variable in a C program lives at a numbered location in memory. A pointer lets your code work with that location directly. You can read or change a variable from inside another function, walk through an array element by element, and later build the linked data structures used throughout systems programming. This lesson covers declaring pointers, the two pointer operators, passing pointers to functions, and pointer arithmetic over arrays.

Addresses and pointer variables

Memory is a long row of bytes, and each byte has an address. An int on a typical 64-bit system occupies 4 bytes, and its address is the address of its first byte. A pointer is a variable whose value is a memory address, together with the type of the object stored at that address. The type matters: it tells the compiler how many bytes to read and how far to move during arithmetic.


Two operators connect a variable and its address. The table uses the values shown in the figure below.


Expression

Read as

Value in the figure

x

the variable x

42

&x

address of x

0x1000

p

the value stored in p

0x1000

*p

the object p points to

42

&p

address of the pointer itself

0x2000


The declaration int *p = &x creates p and stores the address of x in it. After that, the statement *p = 50 writes through the pointer, so x becomes 50 even though the statement never names x. The * in a declaration marks p as a pointer; the * in an expression is the dereference operator, which follows the stored address to the object.


A box for variable x holding 42 at address 0x1000 and a box for pointer p holding 0x1000 at address 0x2000, with an arrow from p to x.



Passing pointers to functions

C passes every argument by value: the function receives a copy. To let a function change a variable that belongs to its caller, pass the variable's address and dereference it inside the function.


C
void swap(int *a, int *b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

int main(void)
{
    int m = 3, n = 8;
    swap(&m, &n);      /* now m == 8 and n == 3 */
    return 0;
}


The pointers a and b are themselves copies, but they hold the addresses of m and n, so writing to *a and *b changes the originals. Without pointers, swap would exchange its own local copies and the caller would see no change. This pattern, usually called pass by reference, is how scanf stores input into your variables and how a function hands back more than one result.

Pointer arithmetic and arrays

An array name used in an expression converts to a pointer to its first element. Adding an integer to a pointer moves it by whole elements, not by bytes:


address of (p+k)=address in p+k×sizeof(∗p)\text{address of } (p + k) = \text{address in } p + k \times \text{sizeof}(\ast p)


The step is scaled by the size of the pointed-to type, so the same p + 2 moves 8 bytes for an int pointer and 16 bytes for a double pointer. Consequently a[i] and *(a + i) are the same expression. Subtracting two pointers reverses the scaling: it gives the number of elements between them, and it is defined only for two pointers into the same array.


Example: If double v[5] starts at address 0x3000 and sizeof(double) is 8, then v + 3 points 3 × 8 = 24 bytes further on, at 0x3018, which is the address of v[3].

What to remember

  • A pointer stores an address, and its type says what object lives there.
  • & takes the address of a variable; * dereferences an address to reach the object.
  • Pass an address when a function must change the caller's variable.
  • p + k moves k elements, which is k * sizeof(*p) bytes.
  • Initialise a pointer before you dereference it.

Example: Tracing pointers through an int array

A program runs the following lines. Assume data begins at byte address 1000 and that sizeof(int) is 4 bytes.


C
int data[6] = {12, 7, 30, 5, 18, 9};
int *p = data + 1;
int *q = &data[4];
*q = *p + 10;
p += 2;


a) Before the last two statements run, what addresses do p and q hold?


Pointer arithmetic scales by the element size. The expression data + 1 moves one int past the start, an offset of ________, so


p=1000+1×4=1004p = 1000 + 1 \times 4 = 1004


The expression &data[4] is the address of the element at index 4, an offset of ________ from the start, so


q=1000+4×4=1016q = 1000 + 4 \times 4 = 1016


So p holds address 1004 and q holds address 1016.


b) Which element does *q = *p + 10; change, and what does the array contain afterward?


The right-hand side is evaluated first. p points to index 1, so *p reads ________ and the sum is 7 + 10 = 17. q points to index 4, so *q = 17 overwrites the 18 stored there. Only that one element changes; p and q themselves still hold 1004 and 1016.


The array is now {12, 7, 30, 5, 17, 9}.


c) After p += 2;, find the address in p, the value of *p and the value of q - p.


p += 2 moves p forward two elements, which is ________:


p=1004+2×4=1012p = 1004 + 2 \times 4 = 1012


Address 1012 is index (1012 − 1000) / 4 = 3, so *p is 5.


Pointer subtraction counts elements, not bytes. The byte gap between q and p is 1016 − 1012 = ________, and dividing by the element size gives


q−p=1016−10124=1q - p = \frac{1016 - 1012}{4} = 1


So p holds 1012, *p is 5 and q - p is 1.

Match each pointer term from C programming with the description that defines it. Each description is used exactly once.

Match each term
A.

Unary * applied to an address, giving access to the object stored there

B.

Unary &, which yields where its operand is stored in memory

C.

A value that refers to no object at all and must never be followed

D.

Adding an integer k to move by k whole elements rather than k bytes

E.

A variable whose value is the memory location of an object of a stated type

Pointer arithmetic

Pointer

Null pointer

Address-of operator

Dereference operator

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A measurement program uses the following C declarations. For each operation, enter yes if it is permitted by the declared type, or no if it violates a const restriction. Consider each operation independently, starting from these declarations.


C
int x = 18;
int y = 24;
const int *p = &(x);
int * const q = &(x);
OperationUsing pUsing q
Read the integer through the pointer
Assign 30 to the integer through the pointer
Redirect the pointer to y

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Consider the following C code. What does the call to printf print?


C
void update(int *x, int y)
{
    *x = *x + y;
    y = 0;
}

int main(void)
{
    int a = 4, b = 11;
    update(&a, b);
    printf("%d %d\n", a, b);
    return 0;
}
Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors