Sub topic 3
Pointer basics: declaring, taking addresses, dereferencing
This chapter assumes you know basic C syntax, can read hexadecimal addresses, and know from the previous chapters that every variable occupies bytes at some address. Here we build the core model of a pointer and the two operators that GATE questions rely on: & and *.
What a pointer is
A pointer is a variable whose value is a memory address. The pointer's type records what kind of object lives at that address. The address says where to look, and the type says how many bytes to read and how to interpret them.
Declaring pointers
A declaration T *p; means that *p has type T. Read it as "p is a pointer to T".
int x = 42;
int *p = &x;The * binds to the declarator, not to the type. This produces a classic trap.
#include <stdio.h>
int main(void) {
int *a, b;
int x = 7;
a = &x;
b = *a;
printf("%zu %zu\n", sizeof a, sizeof b);
return 0;
}Here a is an int * and b is a plain int. On a typical 64-bit system the program prints 8 4. To declare two pointers you must write int *a, *b;.
The two operators
&xis the address-of operator. It yields the address of the objectx, with typeint *whenxis anint.*pis the dereference (indirection) operator. It yields the object thatppoints to.
*p is an lvalue, so you can read it and assign to it.
#include <stdio.h>
int main(void) {
int x = 42;
int *p = &x;
*p = 50;
printf("%d %d\n", x, *p);
printf("%d\n", p == &x);
return 0;
}This prints 50 50 and then 1. Writing through *p changed x, because they name the same storage. The two operators undo each other: *&x is x, and &*p is p, provided p is valid.
The symbol * has two roles that are easy to confuse. In a declaration it is part of the type. In an expression it is the dereference operator. The line int *p = &x; does not dereference anything. It initializes p, not *p.
The type decides how many bytes are read
A pointer value alone does not say how much memory to access. The pointed-to type does. Dereferencing an int * reads sizeof(int) bytes, and dereferencing a char * reads one byte.
#include <stdio.h>
int main(void) {
double d = 1.5;
double *dp = &d;
char *cp = (char *)&d;
printf("%zu %zu %zu\n", sizeof dp, sizeof *dp, sizeof *cp);
return 0;
}On a typical 64-bit system this prints 8 8 1. All object pointers on such a system have the same size, 8 bytes, whatever they point to. The size of *dp is the size of a double, while *cp is one byte. Casting &d to char * is the standard way to inspect an object's bytes.
The expression sizeof *p is not evaluated, so it is safe even if p is uninitialized or null. It only uses the type of *p.
Initialization, NULL, and invalid pointers
A pointer declared without an initializer holds garbage if it is a local variable. Dereferencing it is undefined behavior. Three states are worth separating:
- Valid: it points to a live object.
- Null: it holds the null pointer constant, written
NULLor0in pointer context. It points to no object, and you may compare it but never dereference it. - Invalid: it is uninitialized, or it points to an object whose lifetime has ended. Even reading its value can be undefined behavior in the second case.
#include <stdio.h>
#include <stddef.h>
int main(void) {
int *p = NULL;
if (p == NULL) {
printf("p points to nothing\n");
}
return 0;
}Any pointer can be used as a condition. A null pointer is false and every other pointer is true, so if (p) means if (p != NULL). The null pointer need not be all-zero bits on every machine, but the source constant 0 converts to it.
void pointers
A void * is a generic object pointer. Any object pointer converts to it and back without loss, and C does this without a cast. You cannot dereference a void *, because there is no type to say how many bytes to read. You also should not do arithmetic on it in standard C. The next chapters use it with malloc.
const and pointers
The position of const relative to * decides what is protected. Read the declaration from the name outward.
| Declaration | Can change `*p`? | Can change `p`? |
|---|---|---|
| `int *p` | Yes | Yes |
| `const int *p` | No | Yes |
| `int *const p` | Yes | No |
| `const int *const p` | No | No |
Printing addresses
Use %p and cast the argument to void *. This is the only portable form.
#include <stdio.h>
int main(void) {
int x = 1;
printf("%p\n", (void *)&x);
return 0;
}The printed value differs between runs and machines, so GATE questions never depend on the actual number. They depend on relationships: equal addresses, differences, and what a dereference yields.
Common errors
- Writing
int *a, b;and assuming both are pointers. - Dereferencing before assigning:
int *p; *p = 5;writes to an unknown location. - Confusing
pwith*p. Assigningp = 5changes whereppoints (and is a type error in standard C), while*p = 5changes the pointed-to value. - Assigning between incompatible pointer types, such as
int *todouble *, without a cast. Dereferencing the result violates the type rules. - Returning
&localfrom a function. The local variable ends when the function returns, so the pointer dangles. Chapter on the call stack covers why.
Exercise
What does this fragment print? int a = 10, b = 20; int p = &a; p = b; p = &b; *p = 30; printf("%d %d\n", a, b);
Show answer
It prints "20 30". The assignment p = b copies 20 into a because p points to a. Then p = &b redirects p to b without changing any value, and p = 30 overwrites b.
Exercise
In the declaration int a, b, c; give the type of each of a, b, and c, and say how many of them are pointers.
Show answer
The variable a has type int , b has type int, and c has type int . Two of them are pointers, because each * attaches only to the name that follows it.