Declaring and Using Pointers
You now know what a pointer is. Before using pointers in real programs, you must declare them correctly, give them a safe starting value, and understand why a pointer has a type.
In this lesson you will learn the exact rules for declaring, initializing and dereferencing pointers, plus the traps that cause most pointer crashes in lab exams.
What You Will Learn
- How to declare pointers of different types.
- How to initialize a pointer (with an address or NULL).
- The
int *p, q;trap. - Why a pointer needs a data type.
- The size of a pointer.
- How to read input through a pointer.
Pointer Declaration and Initialization Explained
Exam definition: Declaring a pointer means creating a variable that can hold the address of a given type: type *name;. Initializing it means giving it a valid address (&variable) or NULL. Dereferencing means accessing the value at that address with *name.
Memory trick: D-I-D: Declare it, Initialize it, then Dereference it. Never skip the middle step. An uninitialized pointer is like a GPS with a random destination.
Syntax of Pointer Declaration
int *ip; /* pointer to int */
float *fp; /* pointer to float */
char *cp; /* pointer to char */
double *dp; /* pointer to double */
int x = 10;
int *p = &x; /* declare + initialize with an address */
int *q = NULL; /* declare + initialize to "points nowhere" */
int *a, *b; /* two pointers: star on EACH name */
int *c, d; /* c is a pointer, d is a plain int! */
- The space around
*doesn't matter:int *p,int* pandint * pare the same. - But the
*belongs to the name, not the type. That's whyint* c, d;makes onlyca pointer. Writingint *creminds you of this. NULL(fromstdio.h/stddef.h) means "points to nothing". You can test it:if (q == NULL).
Example 1: Pointers of Different Types
#include <stdio.h>
int main(void)
{
int marks = 88;
float cgpa = 8.7f;
char grade = 'A';
int *pm = &marks;
float *pc = &cgpa;
char *pg = &grade;
int *none = NULL;
printf("marks = %d, cgpa = %.1f, grade = %c\n", *pm, *pc, *pg);
*pm = *pm + 5; /* grace marks */
*pc = 9.1f;
*pg = 'O';
printf("marks = %d, cgpa = %.1f, grade = %c\n", marks, cgpa, grade);
if (none == NULL)
printf("none points to nothing, so we don't dereference it\n");
printf("size of int*, float*, char*: %zu %zu %zu\n",
sizeof(pm), sizeof(pc), sizeof(pg));
return 0;
}
Output
marks = 88, cgpa = 8.7, grade = A marks = 93, cgpa = 9.1, grade = O none points to nothing, so we don't dereference it size of int*, float*, char*: 8 8 8
(The sizes are 8 on a 64-bit system. On an old 32-bit system or Turbo C, you would see 4 or 2.)
Code Explanation
- Each pointer's type matches its variable:
int *for an int,float *for a float,char *for a char. *pm,*pc,*pgread the values. Writing to them changesmarks,cgpaandgrade.noneis initialized toNULL. Checkingnone == NULLbefore using it prevents a crash.- All pointers have the same size, whatever they point to. An address is an address; a
char *isn't smaller than adouble *.
Why Does a Pointer Need a Type?
If all addresses are the same size, why write int * or char *? The type tells C how many bytes to read at that address and how to interpret them.
Address: 1000 1001 1002 1003
Bytes : [ 4 bytes of an int value ]
int *ip = 1000 -> *ip reads 4 bytes (1000-1003) as one int
char *cp = 1000 -> *cp reads 1 byte (1000) as one char
The type also decides how far p + 1 moves, which you will see in Pointer Arithmetic. Pointing an int * at a float gives a warning and wrong values.
Example 2: Reading Input Through a Pointer
#include <stdio.h>
int main(void)
{
int qty;
float price;
int *pq = &qty;
float *pp = &price;
printf("Enter quantity: ");
scanf("%d", pq); /* pq already IS an address: no & */
printf("Enter price: ");
scanf("%f", pp);
printf("Total = %d x %.2f = %.2f\n", *pq, *pp, *pq * *pp);
return 0;
}
Output
Enter quantity: 4 Enter price: 250.50 Total = 4 x 250.50 = 1002.00
scanfneeds an address.pqholds&qty, soscanf("%d", pq)is the same asscanf("%d", &qty).*pq * *pp: the first and third stars dereference; the middle star is multiplication. Spaces make it readable.
Important Points to Remember
- Syntax: type *name;
- Always initialize a pointer: with
&varor NULL. - Put a star on every name:
int *a, *b; - Pointer type must match the variable type.
- All pointers have the same size (8 bytes on 64-bit).
- Never dereference a NULL or uninitialized pointer.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
int* a, b; expecting two pointers | b is a plain int | int *a, *b; |
int *p; printf("%d", *p); | Garbage or crash (wild pointer) | Initialize before use |
float *fp = &marks; (marks is int) | Warning, wrong values | Match the types |
scanf("%d", &pq); with a pointer | Stores into the pointer itself: crash | scanf("%d", pq); |
*p = &x; to point p at x | Stores an address as a value | p = &x; |
Pointer Declaration Practice Questions
- Declare a pointer to a double and make it point to
double pi = 3.14;. - In
char *s, t;, what are the types of s and t? - What is the size of a
char *on a 64-bit system? - Why should a pointer be initialized to NULL if no address is ready?
- What is the difference between
p = &x;and*p = x;?
Show answers
double *dp = π- s is
char *; t ischar. - 8 bytes.
- So it doesn't hold a random address; a NULL check can then catch it before use.
p = &x;makes p point to x.*p = x;copies x's value into whatever p already points to.
Coding Exercise
Declare int a = 6, b = 9; and one pointer p. Use the same pointer to double a, then point it at b and add 1 to b. Print both.
Show solution
#include <stdio.h>
int main(void)
{
int a = 6, b = 9;
int *p = &a;
*p = *p * 2; /* a = 12 */
p = &b; /* now point to b */
*p = *p + 1; /* b = 10 */
printf("a = %d, b = %d\n", a, b);
return 0;
}
a = 12, b = 10
Quick Quiz: Declaring Pointers (MCQ)
-
In
int *x, y;:A) Both are pointers B) Only x is a pointer C) Only y is a pointer D) Error
-
A safe value for a pointer with no target yet:
A) 0.5 B)
NULLC) Any number D)'\0'string -
On a 64-bit system,
sizeof(double *)is:A) 4 B) 8 C) 16 D) Depends on the double value
-
Correct way to read into an int through
int *p = &n;:A)
scanf("%d", &p);B)scanf("%d", *p);C)scanf("%d", p);D)scanf("%p", p); -
The pointer's data type decides:
A) Its own size B) How many bytes
*preads C) The address value D) Nothing
Show answers
- B: only x.
- B:
NULL. - B: 8.
- C:
scanf("%d", p); - B: how many bytes are read (and how they are interpreted).
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Declare | type *name; |
|---|---|
| Initialize | &var or NULL |
| int *a, b; | Only a is a pointer |
| Pointer size | Same for all types (8 bytes on 64-bit) |
| Why a type? | How many bytes to read and how far p + 1 moves |
| scanf with pointer | No &: scanf("%d", p) |
Frequently asked questions
- What is the size of a pointer in C?
- It depends on the system, not the type: usually 8 bytes on 64-bit and 4 bytes on 32-bit.
- Why initialize a pointer to NULL?
- An uninitialized pointer holds a random address; NULL is a known value you can check before use.
- Why do pointers have data types if all addresses are the same size?
- The type tells C how many bytes to read at the address and how pointer arithmetic steps.
Remember
- D-I-D: declare, initialize, dereference.
- Star on every pointer name.
Next Topic
Next lesson: Pointer Arithmetic in C. You will add and subtract with pointers and see why p + 1 moves by the size of the type.