Pointers to Arrays, Functions, and Structures in C++
In the C++ programming language, pointer data type variables can be used to represent complex data structures, such as pointing to a data type variable value object declared in arrays, functions, and structures. Understanding and understanding pointer data values in C++ is crucial for effective memory management, dynamic data structures, and low-level programming.

So, let’s explore pointers to array, function, and structure data types in C++ programming in detail.
Array Pointer Concept in C++.
In a C++ program, a user-defined pointer can point to the location of the first element of an array, and using pointer arithmetic operators, C++ users can access the values of other address elements of the array.
Syntax of an Array Pointer.
Declaring a pointer variable object for an array data type element in a C++ program.
DataType* pointerVariableName;
Array Pointer to point to an array.
pointerVariableName = array;
Example of Array Pointer in C++.
#include <iostream>
int main() {
int array[] = {98, 67, 15, 70, 95, 79}; //here we declare array of integers numbers
int* ptr = array; // Pointer to the first element of the array
// here we are accessing array elements using the pointer data type
std::cout << “1st element of pointer to array – ” << *ptr << std::endl; // Result is – 98
std::cout << “2nd element of pointer to array – ” << *(ptr + 1) << std::endl; // Result is – 67
std::cout << “3rd element of pointer to array -” << *(ptr + 2) << std::endl; // Result is – 15
return 0;
}
Explanation of Array Pointer in C++.
- Here, ptr in an array pointer indicates or points to the first element of the array (arr[0]).
- By incrementing the pointer (ptr + 1), we move to the next array pointer element (arr[1]), and so on.
- The dereference operator (*) symbol in an array pointer is used to access and manage the value at the memory location in which the pointer is pointing.
Important Notes of Array Pointer.
- In a C++ program, the name of an array (e.g., array) is equivalent to a pointer to the first element of the array.
- When an array is passed as a parameter to a user-defined function, it is actually passing a pointer to the first element.
Pointer to Function Concept in C++.
In C++ programming, a pointer data type variable can also point to a user-defined function parameter value. This allows C++ users to indirectly call a function in a program, declare multiple function parameters in a program, pass them as arguments, or store a user-defined function in a data structure.
Pointer to Function Syntax.
Declaring a pointer data type variable for a user-defined function parameter argument in a C++ program.
returnType (*pointerName)(parameterTypes);
Assigning a function to a pointer.
pointerName = &functionName;
Example of a Pointer to Function.
#include <iostream>
// here we define a simple function
int total(int p, int q) {
return p + q;
}
int subtract(int p, int q) {
return p – q;
}
int main() {
// here we are declaring a pointer to a function that takes two integer values and returns an int data
int (*funcPointr)(int, int);
// here we are assigning function addresses to the pointer data type
funcPointr = &total;
// here we are calling the function through the pointer variable
std::cout << “The total is – ” << funcPointr(1, 4) << std::endl; //The total is – 5
//here we are reassigning it to another function
funcPointr = &subtract;
// Here we call the second function through the pointer method
std::cout << “The subtraction is – ” << funcPointr(17, 9) << std::endl; // The subtraction is – 8
return 0;
}
Explanation of a Pointer to Function.
- In this example, int (*funcPointr)(int, int) declares a pointer variable, funcPointr, to a function that takes two int parameter value arguments as input and returns a single int value as output.
- funcPointr = &total; assigns the address value of the total function to the pointer.
- The function is called through a pointer using the funcPointr(1, 4) syntax.
Important Notes of Pointer to Function.
- In a C++ program, a user-defined function pointer can be passed as an argument to another function, enabling callback mechanisms.
- Function pointers can also be used to implement simple dispatch tables (function lookup tables).
Pointer to a Structure Concept in C++.
Pointer data types of structure data types in C++ programming provide C++ users with features like allocating memory for structure data type element members in a dynamic order or referencing an existing structure data type. C++ users can access and manage structure members using a pointer with the use of the -> operator. Use this instead of the . operator for a normal C++ regular structure data type variable.
Syntax of a Pointer to a Structure.
Declaring a pointer data type variable for a structure data type member element in a C++ program.
struct SampleStruct* pointerVariableName;
Accessing its element members using the pointer data type.
pointerVariableName->memberName;
Example of a Pointer to a Structure.
#include <iostream>
struct Student {
std::string stu_name;
int stu_age;
};
int main() {
// here we declare a structure data member and a pointer to the structure data type
Student student = {“Siddhi Deora”, 23};
Student* ptr = &student; //here we define pointer to the structure data type
//here we accessing structure members using the pointer
std::cout << “Student Name is – ” << ptr->stu_name << std::endl; // Result is – Siddhi Deora
std::cout << “Student Age is – ” << ptr->stu_age << std::endl; // Result is – 23
// Here’s another method, using the dereference operator and the dot operator for structure members.
std::cout << “Student Name (from dereferencing) method – ” << (*ptr).stu_name << std::endl; // Result is – Siddhi Deora
std::cout << “Student Age (from dereferencing) method – ” << (*ptr).stu_age << std::endl; // Result is – 23
return 0;
}
Explanation of a Pointer to a Structure.
- In this example, a pointer ptr is initialized to point to a Student structure.
- To access the data members of a structure, we use the ptr->memberName method. Alternatively, we can dereference the pointer using the (*ptr).memberName method.
Important Notes of Pointer to a Structure.
- In a C++ program, the -> operator is a shorthand method for dereferencing a pointer to a structure member and then accessing and managing the structure’s members. If a C++ user has a pointer data type method of a structure, the -> structure to pointer operator is used to access the structure’s data type members. If a C++ user has a structure variable member, they can do so using the . operator.
Combining Pointers with Array, Function, and Structure Data Members.
In C++ programming, users can group pointers to array, function, and structure data types to create more complex data structures and algorithmic programs. Here, we will explore in detail an example of combining pointers to array, function, and structure data types in a C++ program.
Example of an array of structure data members with pointers.
#include <iostream>
struct Student {
std::string stu_name;
std::string stu_course;
int course_fee;
};
int main() {
Student Students[] = {{“Siddhi”,”Java”, 1099}, {“Bhavishi”,”Python”, 1299}, {“Harry”,”Xcode”, 799}, {“Vivek”,”Ruby”, 899}};
Student* pntr = Students; // Here, use a pointer to the first element in the array of structures.
for (int p = 0; p < 4; p++) {
std::cout << “Student Details ” << (p + 1) << ” – ” << pntr->stu_name << “, ” << pntr->stu_course << “, ” << pntr->course_fee << “, course fee.” << std::endl;
pntr++; // Here, move to the next element in the array list.
}
return 0;
}
Explanation of an array of structure data members with pointers.
- In this example, C++ users use the pointer method to move within an array of structure data types. The pntr pointer starts at the first element, and we use the -> operator to access and manage each structure data member.

