Day#55 'C' Coding Challenge

Day#55 'C' Coding Challenge

What is the output of the below code?

#include<stdio.h> char marks[] = {24,45,67}; void main() { char *ptr_marks = marks; if(&ptr_marks == &marks) printf("Both pointers point to same location"); else printf("Both pointers point to different location"); printf("Checking array pointer and normal pointer"); }

----------------------------------------------------------------------------

The above code declared a character pointer "ptr_marks" which is assigned the address of array "marks".
This meant we are assigning, address of marks[0]
Now, in the if condition we are checking if the address of ptr_marks and address of marks are same or not. Definitely, both of them are different. ptr_marks holds the address of marks but the actual address at which ptr_marks is present is different.
So, the code prints both "Both pointers point to different location" and "Checking array pointer and normal pointer"

Few more points,
  • pointer is actually a block of memory which holds address of another memory
  • the amount of memory allocated to pointer to store address depends on the type of pointer
  • the address of array is nothing but the address of the starting element of the array.
  • the address need to be incremented by the address space occupied by data type to point to the next location of the array

Post a Comment

0 Comments