Multi-dimensional array and pointer

Source: Internet
Author: User

When I wrote 3.43, I thought about the answer for two days. I would like to record it.

 

The topic is to compile three different versions of programs so that they can output Ia elements. Version 1 uses the range for statement, version 2 uses the common for + subscript operator, and Version 3 uses the for statement + pointer.

Below are the results of the two-day trial...

#include<iostream>using namespace std;int main(){    int ia[2][3] = {1,2,3,3,2,1};    //version 1    for(int (&i1)[3]:ia){        for(int j1:i1)            cout<<j1<<" ";    };    cout<<endl;         //version 2    for(int i2 = 0;i2 < 2;++i2){        for(int j2 = 0;j2 < 3;++j2)            cout<<ia[i2][j2]<<" ";    }    cout<<endl;        //version 3    for(int (*i3)[3] = ia;i3 != &ia[2];++i3){ //also  for(int (*i3)[3] = begin(ia);i3 != end(ia);++i3){        for(int *j3 = *i3; j3 != *i3+3 ;++j3) //also  for(int *j3 = begin(*i3); j3 != end(j3);++j3)          cout<<*j3<<" ";    }        return 0;}

 

If version 2 is relatively easy to write, I will not repeat it here. It is mainly about version 1 and version 3.

Version 1:

At the beginning, it seems that I directly use an int * pointer to traverse IA, and then I printed only 1 and 3 at the end of the flower line. I did not know it until I wrote it to version 3 ), in the multi-dimensional array {1, 2, 3}, {3, 2, 1}, Ia points to the first element, while IA + 1 points, points to the first element of the second one-dimensional array. Therefore, only one pointer is obviously unable to print all elements. Return to the textbook. There is an example, INT (* P) [4] = IA, P points to an array containing four integers (IA [3] [4] in the textbook). So I was wondering if, once defined in this way, a [4] is added, it will open up a space of four integers for P. Then I added [3] to the initial version of version 1 and a dual loop. Obviously, the second range is a pointer and compilation error... Then, according to the book, I changed * To &, so I finally did not skip error...

PS: there is another problem. In the for loop, the item type can be various, which can be a pointer or Int. Since the book always uses auto to let the compiler recognize it, I mistakenly thought that item can only be a pointer... Actually, it can be a variety of things.

 

 

Version 3:

Version 3 is really tangled. At first I wanted to save time using begin (IA) and end (IA), and I didn't know the problems of IA and IA + 1 at first, the results show that the types in the brackets do not match. Therefore, the begin and end methods are abandoned and the IA and IA + n methods are used. Then I learned about the problem mentioned in the red letter above, so I immediately changed a loop, so OK, a loop no longer jumps error. So the double loop is written as int * J3 = I3 at the beginning. I think it's all pointers. So there should be no problem in writing, so there is another problem... So I continued to change it. I tried to add and add... As a result, I checked the data line again. Mom's egg processes each one-dimensional array, and then adds * to process every element in the one-dimensional array. OK, so the problem is solved. Just now I want to add begin and end, and I find that there is no problem at all. After knowing the two problems of the red letter, I will solve them all at once. It's easy to use begin and end, saving a lot of effort on your fingers!

 

So after the competition, I understood a question. I should study more materials and don't always look for answers... I don't know what people think when I find the answer...

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.