---
title: "Why datatype in pointer declaration"
subject: "Computer Programming"
chapter: 9
key: "semester_1/computer_programming:pointer-datatype-needed"
source: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-datatype-needed
site: BCT Engineering
---

# Why datatype in pointer declaration

Computer Programming · Chapter 9: Pointers

- Asked in: 2070 Chaitra, 3 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-datatype-needed
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-9

## Question

A pointer variable is used to store address of some other variables, however, we need to specify datatype while declaring a pointer variable. Why?

## Short answer

While all pointers store addresses, the data type is required for correct dereferencing (determines how many bytes to read and how to interpret them), for pointer arithmetic that scales by sizeof the pointed type, and for compiler type checking to catch mismatched assignments.

## Answer

All pointers store an address, which has the same size whatever the type. But the data type is still needed for the following reasons:

1. **Dereferencing**: when `*p` is used, the compiler must know how many bytes to read from that address and how to interpret them. A `char *` reads 1 byte, an `int *` reads 4 bytes, a `double *` reads 8 bytes.
2. **Pointer arithmetic**: `p + 1` moves by `sizeof` the pointed type, not by 1 byte. For `int *p`, `p + 1` is 4 bytes ahead, so it reaches the next integer in an array.
3. **Type checking**: the compiler can warn when an address of the wrong type is assigned, e.g. `int *p = &floatVar;`.

Example: if the same 4 bytes are read through `char *` they give one character, but through `int *` they give a whole integer.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. When *p is used, why must the compiler know the pointer's data type?
   A. To know how many bytes to read and how to interpret them
   B. To decide the size of the address
   C. To allocate memory for p
   D. To check the variable name
   Answer: A. A char * reads 1 byte, an int * reads 4 bytes, and so on; the address alone does not say how much to read. (Key point: Why the type is needed on dereference)

2. How does the size of a pointer to char compare with a pointer to double?
   A. char pointer is 1 byte, double pointer is 8
   B. Both are the same size because both store an address
   C. double pointer is always twice as large
   D. It depends on the value pointed to
   Answer: B. All pointers store an address of the same size whatever the type; the type matters for access and arithmetic. (Key point: Pointer size)

3. For int *p, what does p + 1 point to?
   A. The next byte
   B. The next int, 4 bytes ahead
   C. The same address
   D. The value plus 1
   Answer: B. Adding 1 moves by sizeof the pointed type, so the next integer in an array is reached. (Key point: Pointer arithmetic depends on type)

## Related in Computer Programming

- [Variable declaration versus definition](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adeclaration-vs-definition) — Computer Programming, chapter 3
- [Define operator, data type, constant, variable](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adefine-operator-datatype-constant-variable) — Computer Programming, chapter 3
- [Accessing array elements using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-access-array-elements) — Computer Programming, chapter 9
- [Syntax of while loop](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asyntax-while) — Computer Programming, chapter 5
- [Need of control statements](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Awhy-control-statements) — Computer Programming, chapter 5, asked 2×
- [Need of analysis and design](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aanalysis-design-before-coding) — Computer Programming, chapter 2

## Related in other subjects

- [Why do we need DFT](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-need-dft) — Digital Signal Analysis and Processing, chapter 7, asked 6×
- [Why do we need FFT](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-need-fft) — Digital Signal Analysis and Processing, chapter 7, asked 2×
- [Patient information store and retrieve](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apatient-file-program) — Object Oriented Programming, chapter 8
- [Discrete and continuous random variables](https://www.bctengineer.com/question.md?key=semester_5%2Fprobability_and_statistics%3Adiscrete-continuous-rv-mean-variance) — Probability and Statistics, chapter 2
