Pointer
Pointer
The pointer in C language is a variable which stores the address of another variable. This variable
can be of type int, char, array, function, or any other pointer.
#include<stdio.h>
int main(){
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x \n",p); // p contains the address of the number therefore printing
p gives the address of number.
printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a pointer
therefore if we print *p, we will get the value stored at the address contained by p.
return 0;
}
Array of pointer
In C, a pointer array is a homogeneous collection of indexed pointer variables that are references to a memory
location. It is generally used in C Programming when we want to point at multiple memory locations of a similar data
type in our C program.
Syntax:
#include <stdio.h>
int main()
return 0;
}
Structure Pointer
The structure pointer points to the address of a memory block where the Structure is being stored. Like a pointer that
tells the address of another variable of any data type (int, char, float) in memory. And here, we use a structure pointer
which tells the address of a structure in memory by pointing pointer variable ptr to the structure variable.
We can also initialize a Structure Pointer directly during the declaration of a pointer.
#include <stdio.h>
int main()
{
struct Subject sub; // declare the Subject variable
struct Subject *ptr; // create a pointer variable (*ptr)
ptr = ⊂ /* ptr variable pointing to the address of the structure variable sub */
return 0;
} //in this, the sub is the structure variable, and ptr is the structure pointer variable that points to the address of the
sub variable like ptr = &sub. In this way, each *ptr is accessing the address of the Subject structure's member.
Program to access the structure member using structure pointer and arrow (->) operator
#include <stdio.h>
int main()
{
// store the address of the emp1 and emp2 structure variable
ptr1 = &emp1;
ptr2 = &emp2;
printf ("\n Display the Details of the Employee using Structure Pointer");
printf ("\n Details of the Employee (emp1) \n");
printf(" Name: %s\n", ptr1->name);
printf(" Id: %d\n", ptr1->id);
printf(" Age: %d\n", ptr1->age);
printf(" Gender: %s\n", ptr1->gender);
printf(" City: %s\n", ptr1->city);
In the above program, we have created an Employee structure containing two structure variables emp1 and emp2,
with the pointer variables *ptr1 and *ptr2. The structure Employee is having the name, id, age, gender, and city as the
member. All the Employee structure members take their respective values from the user one by one using the pointer
variable and arrow operator that determine their space in memory