C + + Level two pointer second memory model (two-dimensional array)

Source: Internet
Author: User
Tags strcmp

C + + Level two pointer second memory model (two-dimensional array) two-dimensional array

A two-dimensional array is essentially an array of array elements, an array of arrays.

Defined

  Type descriptor array name [constant expression] [constant expression]

For example: float a[3][4],b[5][10]; Two-dimensional array element address
#include <iostream>using namespacestd;intMain () {cout<<"Hello world!"<<Endl; inta[3][4]={        {1,2,3,4},        {5,6,7,8},        {9,Ten, One, A}    }; int*p; //P=a; ErrorP =a[0];//OK     for(intI=0;i< A; i++) cout<<* (p+i) <<"--------"<<p+i<<Endl; cout<<a<<endl;//A and a[0] address are the same.cout<<a[0]<<Endl; return 0;}

You can see that the memory address of the two-dimensional array is contiguous.

Therefore, the stack area is occupied by a contiguous piece of memory.

#include <iostream>using namespacestd;intMain () {inta[3][4]={        {1,2,3,4},        {5,6,7,8},        {9,Ten, One, A}    }; int*p; //P=a; ErrorP =a[0];//OK     for(intI=0;i< A; i++) cout<<* (p+i) <<"--------"<<p+i<<Endl; cout<<"-----------------------"<<Endl; cout<<"A ="<<a<<endl;//find A and a[0] address is the same: 0012ff50cout<<"A[0] ="<<a[0]<<Endl; cout<<"*a ="<<*a<<Endl; //The following 3 values of the elements that were originally thought to be output, the result *a an address, and a value like a//visible two-dimensional array name is a two-level pointer, A is the address of the addresscout<<"-----------------------"<<Endl; cout<<"(*a) ="<< (*a) <<endl;//address of the corresponding element 1 0012ff50cout<<"(* (a+1)) ="<< (* (A +1)) <<endl;//address of the corresponding element 5 0012ff60cout<<"(* (a+2)) ="<< (* (A +2)) <<endl;//address of the corresponding element 9 0012ff70cout<<"-----------------------"<<Endl; cout<<"* (*a) ="<<* (*a) <<endl;//1cout<<"* (* (a+1)) ="<<* (* (A +1)) <<endl;//5cout<<"* (* (a+2)) ="<<* (* (A +2)) <<endl;//9cout<<"-----------------------"<<Endl; cout<<"A[0] ="<<a[0]<<endl;//address of the corresponding element 1 0012ff50cout<<"a[1] ="<<a[1]<<endl;//address of the corresponding element 5 0012ff60cout<<"a[2] ="<<a[2]<<endl;//address of the corresponding element 7 0012ff70    return 0;}

/***********************************************************/

 

Fragment:
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int *p;
P=a; //? A is the first address of this two-dimensional array, but why can't p=a?
cout<<a<< "\ n";
cout<<p<<endl;

Direct "Cout<<a" can see the address is a[0][0]
But why can't p=a? A is not the first address of a two-dimensional array? Why do you have to use p=a[0]?
Ask!

Ans----------->

Different data types
P is a pointer to int
A can be seen as a pointer to int [3]
int and int [3] are not of the same type,
The former is a simple data type, which is an array type consisting of simple data types.
Because the two types of data are different, the pointers to them are of different types.
Pointer operations are performed according to the type of pointer,
So p++ only causes p to move the byte length of an integer,
a++ has moved three integers to the byte length,
The pointer operation shows that the two pointers are not of the same type.

But the coercion between the pointers is generally possible,
Thus can be as follows:
P= (int*) A;

Although the types A and a[0] are different, their values are the same.

But the same value is not necessarily the same data type!

Ans----------->

In an expression other than sizeof, &, and string constants, the array type is automatically converted to pointer type.

For P=a, this sentence, the type of a is array type, that is, before Int[3][3],a is given p, its type is automatically converted to pointer type, that is, int (*) [3], the result of the conversion is a pointer to the array, and the type of P is int*, is a pointer to an integer, both types are incompatible and cannot be directly assigned.

A[0] is also of type array type, which is int[3],

Similarly, a[0] in the expression p=a[0] will be automatically converted to pointer type first,

is int*, compatible with the type of p, so it can be p=a[0].

Ans----------->

The explanations are as follows:
For example
int A[2][4] is a two-dimensional array that contains 8 elements
What does this array represent:
Represented as a array of two elements (can be imagined as a structure, consisting of 4 int)
A is the pointer to this array, which refers to the first element of the two-dimensional array of a, i.e., a[0] (consisting of 4 int)
When you assign a value, the compiler thinks a is a 4-element structure, and p is an int pointer, the type does not match, and therefore the error.
While A[0] is the address of a[0][0], the type of a[0] is int *, which matches p, so it can.

/***********************************************************/ 

Example:
#include"stdio.h"#include"stdlib.h"#include"string.h"voidMain () {inti =0, j =0; Charbuf[ -]; Charmyarray[Ten][ -] =  {"CCCCC","AAAA","bbbb","11111"}; //Print the second memory model     for(i=0; i<4; i++) {printf ("%s \ n", Myarray[i]); }    //Sort     for(i=0; i<4; i++)    {         for(j=i+1; j<4; J + +)        {            if(strcmp (Myarray[i], myarray[j]) >0) {strcpy (buf, myarray[i]);                strcpy (Myarray[i],myarray[j]);            strcpy (Myarray[j], buf); }        }    }    //Print the second memory model     for(i=0; i<4; i++) {printf ("%s \ n", Myarray[i]); } System ("Pause");}

#include"stdio.h"#include"stdlib.h"#include"string.h"//int Array[10]===>int *array===>//int PrintfArr22 (char array[10], int iNum);intPrintfArr23 (Charmyarray[Ten][ -],intiNum) {    inti =0;  for(i=0; i<inum; i++) {printf ("%s \ n", Myarray[i]); }    return 0;}//int PrintfArr22 (char array[10], int iNum);intSortArr23 (Charmyarray[Ten][ -],intiNum) {    inti =0, j =0; Charbuf[ -];//BUF array name represents the address of the first element of the array//Sort     for(i=0; i<4; i++)    {         for(j=i+1; j<4; J + +)        {            if(strcmp (Myarray[i], myarray[j]) >0) {strcpy (buf, myarray[i]);                strcpy (Myarray[i],myarray[j]);            strcpy (Myarray[j], buf); }        }    }}voidMain () {inti =0; Charmyarray[Ten][ -] =  {"CCCCC","AAAA","bbbb","11111"};//what does the myarray array name represent? Throwing Bricks//Print the second memory model     for(i=0; i<4; i++) {printf ("%s \ n", Myarray[i]); } printf ("the second kind of memory model, before ordering \ n"); PrintfArr23 (MyArray,4); //PrintfArr23 (myarray[10][30], 4);SortArr23 (MyArray,4); printf ("second type of memory model, after ordering \ n"); PrintfArr23 (MyArray,4); System ("Pause");}

C + + Level two pointer second memory model (two-dimensional array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.