0

I'm trying to access 1D array as 2D array. But it falls into Segv. Below is snippet the i had written. Can anyone please take a look into this?

void printarray(int **a){


    printf("#####2D access... \n");
    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){

            printf("## %u-->%d \n", &a[i][j],a[i][j]);
        }
        }

}

int main(){
    int a[4] = {10,20,30,40};
    printf("%u %u %u \n", &a, a, &a[0]);
    printf("%u %u %u %u \n", &a[0], &a[1], &a[2], &a[3], &a[4]);
    printarray((int **)a);
    return 0;
}

And vice-versa(Accessing 2D array as 1D array) situation is simply handled by type casting.Below is the snippet and this works fine.

void printarray(int *a){
    printf("#####1D access... \n");
    for(int i=0;i<4;i++){
        printf("## %u-->%d \n", &a[i],a[i]);
    }
}

int main(){
    int a[2][2] = {
        {10,20},{30,40}
    };
    printf("%u %u %u %u \n", &a, a, a[0], &a[0]);
    printf("%u %u \n", a[0], &a[0]);
    printf("%u %u \n", a[1], &a[1]);

    printarray((int *)a);
    return 0;
}

thanks, Hari

4
  • 2
    in your first example, which dimensions of a 2D-array do you expect when having a 1D-array of size 4; shall it be 1x4,2x2, 4x1? The compiler won't guess :-) Commented Jul 27, 2018 at 13:16
  • @StephanLechner ultimately this 2D array(1x4 or 2x2 or 4x1) are stored sequentially, then we can access the any element using the base address.This sown In the second example by typecasting. Similarly in 1st case also why can't i access elements in 2D manner for the 1D array ? Commented Jul 27, 2018 at 13:23
  • 1
    a** is an pointer to a pointer. Not a two dimensional array. a[i][j] is probably trying to use a[i] as a pointer and then getting thejth element after that pointer. It is (a[i])[j] so a[0][1] is going to be ((int*)(10))[1]; on a 32 bit system. Commented Jul 27, 2018 at 13:24
  • @WilliamJBagshaw It something interesting. i didn't get you. Can you please explain this line a[0][1]-->((int*)(10))[1]. My understanding is that a[0][1]-->*((baseaddrof(a)+0)+1). In this how can *(baseaddrof(a)+0) this line might refer to (int)(10) ? Commented Jul 27, 2018 at 13:36

1 Answer 1

2

You want to use reinterpret_cast to get a reference to the data stored in your array:

#include <iostream>

void printarray(int a[2][2])
{
    printf("#####2D access... \n");
    for (int i = 0; i<2; i++)
    {
        for (int j = 0; j<2; j++)
        {

            printf("## %p-->%d \n", &a[i][j], a[i][j]);
        }
    }
}

void printarray(int b[4])
{
    printf("#####1D access... \n");
    for (int i = 0; i<4; i++)
    {
        printf("## %p-->%d \n", &b[i], b[i]);
    }
}

int main()
{
    int a[4] = { 10,20,30,40 };
    int(&arr)[2][2] = reinterpret_cast<int(&)[2][2]>(a);
    printarray(arr);

    int b[2][2] = {{ 10,20 },{ 30,40 }};
    int(&brr)[4] = reinterpret_cast<int(&)[4]>(b);
    printarray(brr);

    return 0;
}

Example: https://ideone.com/thc55R

#####2D access... 
## 0x7fff708c4a90-->10 
## 0x7fff708c4a94-->20 
## 0x7fff708c4a98-->30 
## 0x7fff708c4a9c-->40 
#####1D access... 
## 0x7fff708c4aa0-->10 
## 0x7fff708c4aa4-->20 
## 0x7fff708c4aa8-->30 
## 0x7fff708c4aac-->40 
Sign up to request clarification or add additional context in comments.

6 Comments

thanks for your answer..and I heard that reinterpret cast is dangerous..so any idea to use any other method.?
@nhkrishna Yes, avoid raw arrays all together, use std::array or std::vector instead
I have used one dimensional dynamic array, then 2D accessing output gives different results. Here is the code ideone.com/oWoIAq . Can you please take look into this ?
@nhkrishna a is a pointer and not the array. If you dereference it reinterpret_cast<int(&)[2][2]>(*a); it might work but would probably result in undefined behaviour.
yes, if i dereference the pointer and use the reinterpret_cast then it works perfectly fine. Thank you :-)
|

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.