0% found this document useful (0 votes)
11 views5 pages

Pointer

Uploaded by

singhvivkumar.11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Pointer

Uploaded by

singhvivkumar.11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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:

pointer_type *array_name [array_size];

● pointer_type: Type of data the pointer is pointing to.

● array_name: Name of the array of pointers.

● array_size: Size of the array of pointers.

// C program to demonstrate the use of array of pointers

#include <stdio.h>

int main()

// declaring some temp variables

int var1 = 10;


int var2 = 20;

int var3 = 30;

// array of pointers to integers

int* ptr_arr[3] = { &var1, &var2, &var3 };

// traversing using loop

for (int i = 0; i < 3; i++) {

printf("Value of var%d: %d\tAddress: %p\n", i + 1, *ptr_arr[i], ptr_arr[i]);

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.

Declare a Structure Pointer


The declaration of a structure pointer is similar to the declaration of the structure variable. So, we can declare the
structure pointer and variable inside and outside of the main() function. To declare a pointer variable in C, we use the
asterisk (*) symbol before the variable's name.

struct structure_name *ptr;

Initialization of the Structure Pointer


ptr = &structure_variable;

We can also initialize a Structure Pointer directly during the declaration of a pointer.

struct structure_name *ptr = &structure_variable;

Access Structure member using pointer:


There are two ways to access the member of the structure using Structure pointer:

Using ( * ) asterisk or indirection operator and dot ( . ) operator.


Using arrow ( -> ) operator or membership operator.

#include <stdio.h>

// create a structure Subject using the struct keyword


struct Subject
{
// declare the member of the Course structure
char sub_name[30];
int sub_id;
char sub_duration[50];
char sub_type[50];
};

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 */

strcpy (sub.sub_name, " Computer Science");


sub.sub_id = 1201;
strcpy (sub.sub_duration, "6 Months");
strcpy (sub.sub_type, " Multiple Choice Question");

// print the details of the Subject;


printf (" Subject Name: %s\t ", (*ptr).sub_name);
printf (" \n Subject Id: %d\t ", (*ptr).sub_id);
printf (" \n Duration of the Subject: %s\t ", (*ptr).sub_duration);
printf (" \n Type of the Subject: %s\t ", (*ptr).sub_type);

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>

// create Employee structure


struct Employee
{
// define the member of the structure
char name[30];
int id;
int age;
char gender[30];
char city[40];
};

// define the variables of the Structure with pointers


struct Employee emp1, emp2, *ptr1, *ptr2;

int main()
{
// store the address of the emp1 and emp2 structure variable
ptr1 = &emp1;
ptr2 = &emp2;

printf (" Enter the name of the Employee (emp1): ");


scanf (" %s", &ptr1->name);

printf (" Enter the id of the Employee (emp1): ");


scanf (" %d", &ptr1->id);
printf (" Enter the age of the Employee (emp1): ");
scanf (" %d", &ptr1->age);
printf (" Enter the gender of the Employee (emp1): ");
scanf (" %s", &ptr1->gender);
printf (" Enter the city of the Employee (emp1): ");
scanf (" %s", &ptr1->city);

printf (" \n Second Employee: \n");


printf (" Enter the name of the Employee (emp2): ");
scanf (" %s", &ptr2->name);

printf (" Enter the id of the Employee (emp2): ");


scanf (" %d", &ptr2->id);
printf (" Enter the age of the Employee (emp2): ");
scanf (" %d", &ptr2->age);
printf (" Enter the gender of the Employee (emp2): ");
scanf (" %s", &ptr2->gender);
printf (" Enter the city of the Employee (emp2): ");
scanf (" %s", &ptr2->city);

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);

printf ("\n Details of the Employee (emp2) \n");


printf(" Name: %s\n", ptr2->name);
printf(" Id: %d\n", ptr2->id);
printf(" Age: %d\n", ptr2->age);
printf(" Gender: %s\n", ptr2->gender);
printf(" City: %s\n", ptr2->city);
return 0;
}

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

You might also like