Check The Programming Section
A pointer is a derived data type and special variable and designed to hold the memory address of some object. Here object can be of any data type as basic, derived and user defined type. Knowing a variable address can be great help for certain types of applications in C/C++. A pointer variable requires two types of operators to work with the memory address of some object. These are as follows:
Address of operator (&) and
Value at address or dereferencing operator (*)
The address of operator & is a unary operator, which requires one operand only. The address of operator &, returns the memory address of its operand. To hold the memory address of a particular type of variable, we need to first declare a pointer variable of the same type (same as & 's operand type). Let’s first declare a pointer variable, which can point to a memory block of type int.
int *ptr;
Here ptr is a pointer variable, must be declared by putting * before the varibale name. It indicates the compiler that ptr will old a pointer means address. Here ptr is not a integer but a pointer to an integer. In this case ptr point to a memory block, which can have a value of type int. Here int is called a base type of the pointer. Let’s declare and initialize a variable of type int.
int num = 56;
Here type of num is int and we can assign its memory address to the above declared pointer variable ptr. The most important point is, the type of num and the type of address that ptr can point is the same as int. Let’s assign the address of num to ptr using the unary address of operator (&).
ptr = #
After the above assignment operation the value of the ptr is the memory address of the variable num. To check this, we can print the address of num and the value of ptr using a printf statement.
#include<stdio.h>
int main(){
int num = 56;
int *ptr;
ptr = #
printf("The value of ptr is %u\n", ptr);
printf("The address of num is %u",&num);
}
The value of ptr is 6487572
The address of num is 6487572
The memory address may be different in your computer. But the value of ptr and the address of num will remain the same.
The second pointer operator which is called value “at address” operator is * and it is also an unary operator requiring one operand only. The * operator returns the value of the variable located at the address in which it is pointing.
int k = *ptr;
ptr is holding the memory address of variable num and the above assignment assigns the value of num to k. So we can read this statement as k receives the value at address ptr.
Using this value at address operator we can change the value of num through the pointer variable ptr. For example, to change the value of num using pointer the required statement will be:
*ptr = 50;
Note: Most importantly, when we want to update the value using a pointer, then the value at address operator is part of the l-value of the assignment operator.
The two pointer operators & and * unfortunately looks like bitwise AND and multiplication operator. But these operators have no relationships between them. Both & and * are higher in precedence than all other aithmetic operators except the unary minus.