What is a Pointer?
A pointer is a special kind of variable which can store another variable’s memory address even pointer variable’s address. Basically, a pointer points to a specific memory address in computer memory. A program can jump to that specific address and retrieve the value that store at that address.
In the below picture, we see that in computer memory we can store another memory’s address. Pointer variables are used to do that.
Getting hand’s dirty with the pointer:
Above we mention, Pointers are special variables. I am trying to make thing clear, Pointers are not always pointed to variables they can point to functions, objects(object-oriented programming), Arrays, Structures etc.
int x = 100;
when we write above piece of code we simply tell the compiler that:
- Reserve memory space to hold the integer value.
- Associate the name x with that reserve memory space and store the value 100 in that memory space.
printf("Address: %u ",&x);
Now, if we run above code, we will get the address of variable x. This is because “&” operator reveals the address of x. We often see this operator in scanf function because of scanf function needs variable’s address to store value.
There is another pointer operator available in C/C++ is ‘*’ called ‘value at address’ operator. It gives the value stored at a particular address.The ‘value at address’ operator is also called ‘indirection’ operator.
printf("Value: %d ",*(&x));
If we run above code we will get the value of variable x. This is because “&” operator reveals the address of x. At the same time “*” or “value at address” operator gives the value of that particular address of x.
Declaring pointer variables:
Declaring a pointer variable is very similar to declaring an ordinary variable except we have to provide an extra “*” operator after specifying the variable’s type.
char* ch; int* in; float* flt; double* doub;
In the above code, we declare pointer variables which can store variable’s address depending on its types.
Now, try to assume below code’s output:
char* ch; int* in; float* flt; double* doub; printf("Size in bytes: %u ",sizeof(ch)); printf("\nSize in bytes: %u ",sizeof(in)); printf("\nSize in bytes: %u ",sizeof(flt)); printf("\nSize in bytes: %u ",sizeof(doub));
I know you are surprised after seeing the output because each pointer variable is same in size.each 4 bytes in size.
Now, you are probably thinking that why each pointer variables is same in size this is because of the address of a computer for any types of data whether its integer or float is same in size.
You are also thinking that then why we need different types of pointer variables if all the pointer variable’s size is same.
printf("Size in bytes for characters: %u ",sizeof(char)); printf("\nSize in bytes for integer: %u ",sizeof(int)); printf("\nSize in bytes for floating number: %u ",sizeof(float)); printf("\nSize in bytes double: %u ",sizeof(double));
if you execute above lines of code.you will find that they are different in size.char is 1 byte, an integer is 4 bytes, a float is 4 bytes, double is 8 bytes.
Computer’s smallest block of memory is 1 byte. So, Integer is combined with 4 blocks of memory and double is combined with 8 blocks of memory.
Pointer variables always point to the first block of memory of particular datatypes. So, if we don’t specify the pointer variables type. A compiler will never find out how many bytes are combined to form the particular data which are referred by the pointer variables. For example, we consider integer datatype. It is combined with 4 blocks of memory. Now, if we try to refer integer types of data with pointer we need to specify the types of the pointer is an integer. Otherwise, Compiler will never know how many bytes are used to form integer type data. Actually, pointer variables types tell the compiler how much memory blocks used to form that particular data which is referred by the pointer variables.
Storing address of a variable in a pointer variable:
int *ptr; int temp=1789; ptr=&temp; // here we store address of variable temp in Ptr pointer variable printf("Address of temp variable %u : ",ptr); // here we see the address of temp //variable stored by ptr pointer variable printf("Value of temp : ",*ptr); // here we the the value of variable //temp (referenced by ptr variable) printf("Address of pointer vaiable ptr : ",&ptr); // here we see pointer variable //address (not temp variable address)
In the above section, we declare an integer pointer variable ptr and then stored temp variable address. To store address in ptr pointer variable we used “&” operator. Because “&” reveals variable address which I discussed above.
Same as above, we can declare any type of pointer variable and store the same type of variable address.
Changing the value of the referenced variable:
int *ptr; int temp=1789; ptr=&temp; // here we store address of variable temp in Ptr pointer variable // before changing the value printf("Value of temp : ",temp); // here we the the value of variable //temp (referenced by ptr variable) *ptr=678; // here we changed the value of temp variable // after changing the value printf("Address of pointer vaiable ptr : ",temp);
In the above code, we changed the value of temp variable with this statement “*ptr=678;“. Here “*” operator used to dereference the address which is stored by ptr variable. When the address is dereferenced we have the power to manipulate the value at that particular address.
Till now we only talk about type defined pointer variable. But there is also a void pointer variable. Actually void means no type. This type of pointer can point to any type of data. But we need to cast it before manipulating thing with it.
void * ptr; printf("\nSize in bytes: %u ",sizeof(ptr));
Above code proves that void pointer variable also 4 bytes.
int x =900; void *ptr=&x; printf("value of x : %u",*((int *)ptr));
In the above code, we declare a void pointer variable ptr and initialize with x variable’s address. Then we print the value of x with ptr pointer variable. Notify that, Before we dereference ptr variable we have to cast void pointer into an integer pointer variable.
Thanks for reading.I hope you find this article helpful. For any Information and Suggestion feel free to comment.