0% found this document useful (0 votes)
4 views

Array of Pointers

The document explains the concept of pointer arrays in C, detailing their syntax and providing examples for both integer and character pointer arrays. It also covers structures, including their declaration, definition, member access, and initialization methods, along with examples. Additionally, it discusses pointers to structures and how to access structure members using pointers.

Uploaded by

thotasuzivasu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Array of Pointers

The document explains the concept of pointer arrays in C, detailing their syntax and providing examples for both integer and character pointer arrays. It also covers structures, including their declaration, definition, member access, and initialization methods, along with examples. Additionally, it discusses pointers to structures and how to access structure members using pointers.

Uploaded by

thotasuzivasu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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

Array of Pointers to Character


One of the main applications of the array of pointers is to store multiple strings as an array of pointers
to characters.
Syntax:
char *array_name [array_size];

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

2. Structure Variable Declaration after Structure Template


struct structure_name variable1, variable2, .......;
Ex:

struct employee
{ int id;
char name[50];
float salary;
};

struct employee e1, e2;

Access Structure Members


We can access structure members by using the ( . ) dot operator.
Syntax
structurevariable.member;

Ex:
e1.id=20;
e1.name=”abc”;

Initializing Structure Members


We can initialize structure members as follows:
1. Using Assignment Operator.
2. Using Initializer List.

1. Initialization using Assignment Operator


struct structure_name str;
str.member1 = value1;
str.member2 = value2;
str.member3 = value3;
.
.
.

Ex:
struct student s1
s1.id=10;
s1.name=”xyz”;
s1.perc=78.5;

2. Initialization using Initializer List


struct structure_name str = { value1, value2, value3 };

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.

Declaration of Pointer to a Structure

You can declare a pointer to a structure (or structure pointer) as follows −

struct Books *struct_pointer;

Initialization of Pointer to a Structure

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;

Let's store the address of a struct variable in a struct pointer variable.

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;

Accessing Members Using Pointer to a Structure

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

int main (){


struct book book1 = {"Learn C", "Dennis Ritchie", 675.50, 325};
struct book *strptr;
strptr = &book1;

printf("Title: %s \n", strptr -> title);


printf("Author: %s \n", strptr -> author);
printf("Price: %lf \n", strptr -> price);
printf("Pages: %d \n", strptr -> pages);
return 0;
}

You might also like