0
#include <iostream>
using namespace std;

int main(){
    int arr[4] = {1,2,3,4};
    int arr2[4] = {5,6,7,8};
    int **arrPtr;

    arrPtr[0] = arr;
    arrPtr[1] = arr2;

    for (int *a : arrPtr ){
        for (int i : a){
            cout << i << endl;
        }
    }
}

I know this implementation is not the way to go but to show the intension at the end. Basically I am trying to print the content of arrays listed by another double pointer. Is there any way to make this code work?

3
  • 7
    That's not an array of arrays, that's just erroneous code. *arrPtr doesn't point anywhere, so assigning to it is illegal. Commented Jan 28, 2014 at 15:40
  • Are you expecting 8 lines of output (4 + 4) or 16 (4 * 4)? Also, did you know that ranged for works on arrays? for (int a : arr){cout << a << endl;} will give you 1 2 3 4 with no pointers in sight Commented Jan 28, 2014 at 15:47
  • I just try to learn c++11 not much and revise some pointer... I just try to print out all of the items below one another Commented Jan 28, 2014 at 15:52

2 Answers 2

3
#include <iostream>

int main() {
    int m[][4] = {
                    { 0, 1, 2, 3 },
                    { 4, 5, 6, 7 },
                    { 8, 9, 0, 1 },
                    { 2, 3, 4, 5 },
                 };

    for(auto &line : m) {
        for(auto &value : line) {
            std::cout << value << ' ';
        }
        std::cout << std::endl;
    }
}

http://coliru.stacked-crooked.com/a/d603241402fcc886

bonus

notice that this also work:

#include <iostream>

int main() {
    for(auto &line : (int [][4])
            {
                { 0, 1, 2, 3 },
                { 4, 5, 6, 7 },
                { 8, 9, 0, 1 },
                { 2, 3, 4, 5 },
            }) {
        for(auto &value : line) {
            std::cout << value << ' ';
        }
        std::cout << std::endl;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, would you explain what is that auto and referencing ?
@Erogol it's c++11 type deduction feature, so that you don't need to state types explicitely when the compiler should already have this figured out. You are asking the compiler: you know what type it's, so I want line and value of that type. Using a reference variable (auto &) is necessary, because without it, line would be just a pointer without array size information, which is required for range-for. check this: stackoverflow.com/questions/20982514/…
0

The new range-for loop does not work on raw pointer types as they don't contain size information. If you know their lengths in compile time, you can cast them as pointers to arrays and use in range-for. Such code may be good to play with but use std::array, std::vector for real uses.

#include <iostream>
#include <memory>
using namespace std;

int main(){
    int arr[4] = {1,2,3,4};
    int arr2[4] = {5,6,7,8};
    unique_ptr<int*[]> arrPtrHolder(new int*[2]);
    int **arrPtr = arrPtrHolder.get();

    arrPtr[0] = arr;
    arrPtr[1] = arr2;

    for (int *a : *(int*(*)[2])(arrPtr)){
        for (int i : *(int (*)[4])(a)){
            cout << i << endl;
        }
    }
}

http://coliru.stacked-crooked.com/a/a207f5c4853b1e1f

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.