Cursor and Array in C ++
Pointers and Arrays are closely related. In fact, pointers and arrays are interchangeable in some cases. For example, a pointer that points to the array head can access that array using either an arithmetic pointer or an array index. You consider the following example:
#include using namespace std ; const int MAX = 3 ; int main () { int mang [ MAX ] = { 10 , 100 , 200 }; int * contro ; // chung ta co mot mang dia chi trong con tro. contro = mang ; for ( int i = 0 ; i < MAX ; i ++) { cout << "Dia chi cua mang[" << i << "] = " ; cout << contro << endl ; cout << "Gia tri cua mang[" << i << "] = " ; cout << * contro << endl ; // tro toi vi tri tiep theo contro ++; } return 0 ; }
Running the above C ++ program will produce the following results:
However, pointers and arrays are not completely interchangeable. For example, consider the following program:
#include using namespace std ; const int MAX = 3 ; int main () { int mang [ MAX ] = { 10 , 100 , 200 }; for ( int i = 0 ; i < MAX ; i ++) { * mang = i ; // Day la cu phap chinh xac mang ++; // cu phap nay la sai . Ban nen chu y. } return 0 ; }
Applying the cursor operator * to the bearing variable is perfect, but it is not valid when modifying the bearing variable value. The reason is that the variable is a constant that points to the beginning of the array and cannot be used as l-value.
Because, an array name creates a pointer constant, it can still be used in pointer expressions, as long as it is not modified. The following example is a valid command that assigns [2] a value of 500.
*( mang + 2 ) = 500 ;
The above command is valid and will successfully compile because the bearing has not been changed.
According to Tutorialspoint
Previous lesson: Cursor pointer in C / C ++
Next article: Array of pointers in C ++
You should read it
May be interested
- Arithmetic pointer in C / C ++as explained in the main chapter, the cursor in c / c ++ is an address, which is a numeric value. therefore, you can perform arithmetic operations on a pointer as you would with numeric values. there are 4 arithmetic operators that can be used on pointers: ++, -, +, and -.
- Array in Language Cthe c programming language provides a data structure called an array, stored in a set of data of the same type with fixed length. an array is used to store data sets, but it is useful if you think of an array of variables with the same type.
- How to copy one array into another array in Golangin go, an array is a fixed-length sequence containing elements of a specific type. copying one array to another is simple but requires both arrays to be of the same length and type.
- Instructions for creating mouse pointer highlights on Windowsthe cursor highlighter application will create a highlight effect for the mouse cursor on your computer, highlighting the cursor, making it easier for you and your viewers to follow during presentations, tutorials and live streams.
- A brand new AI cursor is coming to Windows 11according to the plan, microsoft will announce 'ai explorer' along with pcs running windows 11 ai on may 21. a recent leak shows a new cursor designed specifically for ai actions.
- Steps to customize the cursor with Custom Cursor in Google Chromethe google chrome browser does not have a built-in option to change the cursor icon, but users can use third-party integrations to set it up.
- Cursor in Cpointer in the c language is easy to learn. some tasks in c language are made easier by pointers, and other tasks become more flexible, such as in memory allocation, which cannot be performed without using a cursor.
- Array formulas in Excel - Tutorials and examplesarray 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 JavaScriptin 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 PHPan array is a data structure that stores one or more of the same value types in a single value.