A pointer is a variable that stores the address of another variable. Since every variable is stored at some memory location, a pointer can be used to keep the address of that location and access the value stored there.
A pointer is a variable that stores the address of another variable. Since every variable is stored at some memory location, a pointer can be used to keep the address of that location and access the value stored there.
Here, age stores the value 20, while
ptr stores the address of age.
Whenever a variable is declared, the computer allocates a memory location for it. The variable has a value, and that memory location has an address. The actual address may be different each time the program runs, so the important thing is the relationship between the variable and its address.
The value 75 is stored somewhere in memory and that
memory location has an address.
The address of a variable is the memory location where its value is stored. In C, the address of a variable can be obtained using the address-of operator (&).
The expression &marks gives the address of the
variable marks.
The & operator is called the address-of operator. It returns the memory address of the variable placed after it. This address can then be stored in a pointer variable of the appropriate type.
&number represents the address of
number, not the value 10.
A pointer variable is a variable specifically designed to store a memory address. The type of the pointer tells the compiler what kind of value is expected at the address stored in that pointer.
Here, ptr is a pointer to an integer. It can store
the address of an int variable.
The * operator can be used to obtain the value stored at the address contained in a pointer. It is called the value-at-address or indirection operator.
*ptr gives the value stored at the address held by
ptr, which is 10.
A normal variable stores a value, while a pointer stores the
address of that variable. Using the pointer with the
* operator allows the program to reach the value
stored at that address.
Pointers allow a program to work directly with memory addresses. They are useful when a program needs to access or modify data indirectly, pass addresses to functions, work with arrays and strings, or manage memory dynamically.
Instead of passing the value of a variable to a function, a program can pass its address so that the function can work with the original variable.
The & operator is used to obtain an address, whereas the * operator is used to access the value stored at an address held by a pointer. Understanding this difference is the foundation of pointer programming.
| Expression | Meaning |
|---|---|
&i |
Address of variable i. |
p |
Address stored in pointer p. |
*p |
Value stored at the address held by p. |
*( &i ) |
Value stored at the address of i. |
Write a C program to demonstrate the relationship between a variable, its address, a pointer, and the value stored at the pointer's address.
&.* to access a value.Declare an integer variable, store its address in an integer pointer, and display the address and value through both the variable and the pointer.
A pointer stores the address of another variable. The address-of operator gives the address of a variable, while the indirection operator accesses the value stored at that address.
Value of number = 25
Address of number = 0x7ffe12345678
Value stored in ptr = 0x7ffe12345678
Value at address stored in ptr = 25
The address shown in the output is only an example. The actual
address will normally be different each time the program runs.
What remains important is that ptr contains the same
address as &number, and *ptr gives
the value stored there.
The pointer type should correspond to the type of data whose address it is intended to store. This tells the compiler how the pointed-to data should be interpreted.
p points to an integer,
ch points to a character, and
f points to a floating-point value.
A pointer stores an address, not the ordinary data value itself.
For example, assigning number directly to a pointer
is not the correct way to make the pointer point to
number. Its address should be assigned instead.
Correct:
ptr = &number;
Incorrect:
ptr = number;
| Concept | Remember |
|---|---|
| Pointer | A variable that stores an address. |
| & | Address-of operator. |
| * | Value-at-address / indirection operator. |
| int *p | Pointer to an integer. |
| p = &i | Stores the address of i in p. |
| *p | Accesses the value at the address stored in p. |
int *p mean?*p represent?Watch a beginner-friendly explanation of pointers in C.
A short handwritten-style revision sheet for pointer basics will be provided here.
Use the mind map for variable, address, pointer, & and * relationships.