Array of pointers in C ++

Before we understand the concept of pointer arrays, we consider the following example, which uses an array of 3 integer numbers:

 #include using namespace std ; const int MAX = 3 ; int main () { int var [ MAX ] = { 10 , 100 , 200 }; for ( int i = 0 ; i < MAX ; i ++) { cout << "Gia tri cua var[" << i << "] = " ; cout << var [ i ] << endl ; } return 0 ; } 

When the above code is compiled and executed, it gives the following result:

 Gia tri cua var [ 0 ] = 10 Gia tri cua var [ 1 ] = 100 Gia tri cua var [ 2 ] = 200 

There is a situation when we want to maintain an array, which can store pointers to an int or char data type or any other type. Following is the declaration of an array of pointers to an integer:

 int * contro [ MAX ]; 

It declares the contro as an array of MAX pointer integers. Therefore, each element in contro, now holds a pointer to an int value. The following example uses 3 integer numbers, which will be stored in an array of pointers as follows:

 #include using namespace std ; const int MAX = 3 ; int main () { int var [ MAX ] = { 10 , 100 , 200 }; int * contro [ MAX ]; for ( int i = 0 ; i < MAX ; i ++) { contro [ i ] = & var [ i ]; // gan dia chi cua so nguyen. } for ( int i = 0 ; i < MAX ; i ++) { cout << "Gia tri cua var[" << i << "] = " ; cout << * contro [ i ] << endl ; } return 0 ; } 

When the above code is compiled and executed, it gives the following result:

 Gia tri cua var [ 0 ] = 10 Gia tri cua var [ 1 ] = 100 Gia tri cua var [ 2 ] = 200 

You can use an array of pointers to characters to store a list of strings as follows:

 #include using namespace std ; const int MAX = 4 ; int main () { char * tensv [ MAX ] = { "Nguyen Thanh Tung" , "Tran Minh Chinh" , "Ho Ngoc Ha" , "Hoang Minh Hang" , }; for ( int i = 0 ; i < MAX ; i ++) { cout << "Gia tri cua tensv[" << i << "] = " ; cout << tensv [ i ] << endl ; } return 0 ; } 

Running the above C ++ program will produce the following results:

Array of pointers in C ++ Picture 1

According to Tutorialspoint

Previous article: Cursor and Array in C ++

Next article: Pointers to pointers in C ++

4 ★ | 1 Vote

May be interested

  • How to Create Pointers in CHow to Create Pointers in C
    pointers are the nightmare of every new c programmer. however, they are also the feature that made c the widespread, powerful programming language it is until today. like many other programming features and constructs, there is a...
  • Array formulas in Excel - Tutorials and examplesArray formulas in Excel - Tutorials and examples
    array formulas in excel - tutorials and examples. an array formula is a formula that can perform multiple calculations of one or more items in an array. array formulas can return multiple values ​​or a single result.
  • Learn about Collection of Record in JavaScriptLearn about Collection of Record in JavaScript
    in the previous article, we introduced you to a couple of 2-dimensional array features - 2d array in javascript, in many cases applied when we need to keep information about 1 or more lists of numbers of strings certain data, array object will be the most commonly used and most used tool ...
  • Array (Array) in PHPArray (Array) in PHP
    an array is a data structure that stores one or more of the same value types in a single value.
  • Instructions and examples of array formulas Array Formulas in ExcelInstructions and examples of array formulas Array Formulas in Excel
    the following article details array formulas and practical examples in excel to help you better understand and apply them in your work most effectively.
  • Top 10 best array games for PCTop 10 best array games for PC
    top 10 best array games for pc you prefer to direct and command soldiers to occupy new bases and lands. simulate epic battles in history through strategy games, arrayed games. let's tipsmake.com find the top
  • Cursor to class in C ++Cursor to class in C ++
    a pointer to a class in c ++ is executed in the same way as a pointer to a structure; and to access members of a pointer to a class you use the member access operator in c ++ as the -> operator, as you do with pointers to the structure.
  • How to Print an Array in JavaHow to Print an Array in Java
    if you are working on java and have an array with a large amount of data, you may want to print certain elements in order to view them conveniently. there are multiple ways you can print arrays in java and the examples given below will...
  • Indexer in C #Indexer in C #
    indexer in c # helps index objects, such as an array. when you define an indexer for a class, this class operates similarly to a virtual array. then you can access the instance of this class with the array access operator in c # ([]).
  • The Match function (the function searches for a specified value in an array or cell range) in ExcelThe Match function (the function searches for a specified value in an array or cell range) in Excel
    the match function helps you search for a specified value in an array, a range of cells, and then returns the position of the value in that array or range.