Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 824375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:05:34+00:00 2026-05-15T03:05:34+00:00

Possible Duplicate: casting char[][] to char** causes segfault? I have a 2D array declared

  • 0

Possible Duplicate:
casting char[][] to char** causes segfault?

I have a 2D array declared like this:

int arr[2][2]={ {1,2},{3,4}};

Now if I do:

int ** ptr=(int**) arr;

and:

cout<<**ptr;

I am getting a segmentation fault (using g++-4.0).

Why so? Shouldn’t it be printing the value 1 (equal to arr[0][0])?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:05:34+00:00Added an answer on May 15, 2026 at 3:05 am

    You are attempting to assign a double pointer variable to an array… this has been covered exhaustively, see here for information on this. Furthermore, since you declared

    int arr[2][2] = ...;
    

    and then try to assign arr to a double pointer

    int ** ptr = ... ;
    

    which is guaranteed to not work, hence a segmentation fault. Furthermore, that statement int ** ptr=(int**) arr; is actually cast ing one type (i.e. [][]) to another type (i.e. **) despite they are of type ‘int’. They are both different and the compiler will interpret that very differently…

    You could do it this way:

    int *ptr = &arr;
    

    Now *(ptr + 1) will refer to the 0’th row, *(ptr + 2) will refer to the 1’st row and so on. The only onus on you is to not overstep the markers of where arr is used otherwise an overflow can happen or even a segmentation fault…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Problem in calculating checksum : casting int to signed int32 This should
Possible Duplicate: casting vs using the 'as' keyword in the CLR I have seen
Possible Duplicate: Casting: (NewType) vs. Object as NewType Say for example I have a
Possible Duplicate: Casting vs using the ‘as’ keyword in the CLR Which method is
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Casting: (NewType) vs. Object as NewType Casting vs using the 'as' keyword
Possible Duplicate: casting vs using the ‘as’ keyword in the CLR hi, can somebody
Possible Duplicate: array_splice() for associative arrays How to add an array value to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.