Array of Pointers
Array of Pointers
A pointer array is a homogeneous collection of indexed pointer variables that are references to a
memory location. It is generally used 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];
Here,
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.
Ex:
#include <stdio.h>
#include <conio.h>
void main()
{
int var1 = 10;
int var2 = 20;
int var3 = 30;
// array of pointers to integers
int* ptr_arr[3] = { &var1, &var2, &var3 };
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;
}
#include <stdio.h>
int main()
{
char* arr[] = { "hello", "cse"};
for (int i = 0; i < 2; i++)
{
printf("%s\n", arr[i]);
}
for (int i = 0; i < 2; i++)
{
printf("Address of arr[%d]: %p\n", i, (void*)arr[i]);
}
return 0;
}
Structures
The structure is a user-defined data type that can be used to group items of possibly different
types into a single type.
The struct keyword is used to define the structure.
The items in the structure are called its members and they can be of any valid data type.
Structure Declaration
We have to declare structure before using it in our program. In structure declaration, we specify its
member variables along with their datatype. We can use the struct keyword to declare the structure.
Syntax
struct structure_name
{
data_type member_name1;
data_type member_name1;
....
....
};
Ex:
struct student
{
int id;
char name[20];
float perc;
}
Structure Definition
We can define structure variables using two methods:
1. Structure Variable Declaration with Structure Template
struct structure_name
{
data_type member_name1;
data_type member_name1;
....
....
}variable1, varaible2, ...;
Ex:
struct employee
{ int id;
char name[50];
float salary;
}e1,e2;
struct employee
{ int id;
char name[50];
float salary;
};
Ex:
e1.id=20;
e1.name=”abc”;
Ex:
struct student s1
s1.id=10;
s1.name=”xyz”;
s1.perc=78.5;
In this type of initialization, the values are assigned in sequential order as they are declared in the
structure template.
Ex:
struct student s1={10,”xyz”,78.5};
EX:
#include<stdio.h>
#include<conio.h>
struct student
{
int id;
char name[20];
float perc;
}
int main
{
struct student s1={10,”xyz”,78.5};
struct student s1={20,”abc”,81.5};
printf(“details of student1”);
printf(“student id=%d\t,student name=%s\t,student perc=%f”,s1.id,s1.name,s1.perc);
printf(“details of student2”);
printf(“student id=%d\t,student name=%s\t,student perc=%f”,s2.id,s2.name,s2.perc);
return 0;
}
Pointers to Structures
You can define pointers to structures in the same way as you define pointers to any other variable.
You can store the address of a structure variable in the above pointer variable struct_pointer. To find the
address of a structure variable, place the '&' operator before the structure's name as follows −
struct_pointer = & book1;
struct book{
char title[10];
char author[20];
double price;
int pages;
};
struct book book1 = {"Learn C", "Dennis Ritchie", 675.50, 325},
struct book *strptr;
To access the members of a structure using a pointer to that structure, you must use the → operator as
follows −
struct_pointer->title;
defines the → symbol to be used with struct pointer as the indirection operator (also called struct
dereference operator). It helps to access the elements of the struct variable to which the pointer
reference to.
Example
In this example, strptr is a pointer to struct book book1 variable. Hence, strrptr→title returns the title,
just like book1.title does.
#include <stdio.h>
#include <string.h>
struct book{
char title[10];
char author[20];
double price;
int pages;
};