What is a Pointer in C? Definition, Syntax and First Program
A pointer in C is a variable that stores the memory address of another variable. Learn declaration, the & and * operators and your first pointer program.
What is a pointer?
Think of memory as a colony of houses. A normal variable is a house containing a value. A pointer is a slip of paper on which the house address is written. Using the address, you can reach the house and read or change what is inside.
Declaration syntax
data_type *pointer_name;
int *p; /* pointer to int */
float *fp; /* pointer to float */
char *cp; /* pointer to char */
The * in the declaration says "this variable holds an address of that type", it is not multiplication here.
The two key operators: & and *
| Operator | Name | Meaning |
|---|---|---|
&x | Address-of | Gives the memory address of variable x |
*p | Dereference / value-at | Gives the value stored at the address held in p |
Your first pointer program
#include <stdio.h>
int main() {
int marks = 85;
int *p = &marks; /* p stores the address of marks */
printf("Value of marks : %d\n", marks);
printf("Address of marks : %p\n", (void*)&marks);
printf("Value in p : %p\n", (void*)p);
printf("Value at *p : %d\n", *p);
*p = 95; /* change marks THROUGH the pointer */
printf("New marks : %d\n", marks);
return 0;
}
Notice the last two lines: we never touched marks directly, yet its value changed to 95 — because *p = 95; wrote into the same memory location.
Why do pointers exist? (4 real reasons)
- Call by reference: a function can modify the caller's variable — this is how
scanf("%d", &x)works. - Dynamic memory:
malloc()returns a pointer; without pointers there is no runtime memory allocation. - Arrays and strings: array names decay to pointers; all string handling is pointer-based.
- Data structures: linked lists, trees and graphs are impossible without pointers connecting nodes.
Pointer क्या है?
Memory को घरों की एक colony समझें. Normal variable एक घर है जिसमें value रहती है. Pointer एक पर्ची है जिस पर उस घर का address लिखा है. Address से आप घर तक पहुंचकर अंदर की चीज़ पढ़ या बदल सकते हैं.
Declaration syntax
data_type *pointer_name;
int *p; /* pointer to int */
float *fp; /* pointer to float */
char *cp; /* pointer to char */
Declaration में * कहता है "यह variable उस type का address रखता है" — यहां यह multiplication नहीं है.
दो key operators: & और *
| Operator | Name | मतलब |
|---|---|---|
&x | Address-of | Variable x का memory address देता है |
*p | Dereference / value-at | p में रखे address पर stored value देता है |
आपका पहला pointer program
#include <stdio.h>
int main() {
int marks = 85;
int *p = &marks; /* p में marks का address */
printf("Value of marks : %d\n", marks);
printf("Address of marks : %p\n", (void*)&marks);
printf("Value in p : %p\n", (void*)p);
printf("Value at *p : %d\n", *p);
*p = 95; /* pointer के THROUGH marks बदलें */
printf("New marks : %d\n", marks);
return 0;
}
आखिरी दो lines देखें: हमने marks को direct touch नहीं किया, फिर भी value 95 हो गई — क्योंकि *p = 95; ने उसी memory location में लिखा.
Pointers क्यों होते हैं? (4 real कारण)
- Call by reference: function caller का variable modify कर सकता है —
scanf("%d", &x)ऐसे ही काम करता है. - Dynamic memory:
malloc()pointer return करता है; pointers के बिना runtime memory allocation नहीं. - Arrays और strings: array names pointers में decay होते हैं; सारी string handling pointer-based है.
- Data structures: linked lists, trees और graphs — nodes को जोड़ने वाले pointers के बिना impossible हैं.
Frequently Asked Questions
What is a pointer in C in one line?
A pointer is a variable that stores the memory address of another variable, allowing indirect access to its value.
What do & and * mean with pointers?
& gives the address of a variable (address-of operator), and * gives the value stored at an address (dereference operator).
What is the size of a pointer in C?
The size depends on the system, not the data type: typically 8 bytes on 64-bit systems and 4 bytes on 32-bit systems, same for int*, char* and float*.