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 8905099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:18:24+00:00 2026-06-15T02:18:24+00:00

I know that for single-dimensional arrays x=a[i] is equivalent to x=*(a+i) , but how

  • 0

I know that for single-dimensional arrays x=a[i] is equivalent to x=*(a+i), but how can I access elements of a two-dimensional arrays using pointers?

  • 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-06-15T02:18:25+00:00Added an answer on June 15, 2026 at 2:18 am

    Summary: If you have a multidimensional array defined as int [][], then x = y[a][b] is equivalent to x = *((int *)y + a * NUMBER_OF_COLUMNS + b);


    Boring Details:

    The (int *) cast of y above deserves some explanation, as its necessity may not be at-first intuitive. To understand why it must be there consider the following:

    1. Typed pointer arithmetic in C/C++ always adjusts the typed pointer value (which is an address) by the size of the type in bytes when adding/subtracting/incrementing/decrementing by scalar.

    2. The fundamental type of a multi-dimensional array declaration (not the element type; the variable type) is an array-type of one-less dimension than the final dimension.

    The latter (#2) of these really needs an example to solidify. In the following, variables ar1 and ar2 are equivalent declarations.

    int ar1[5][5]; // an array of 5 rows of 5 ints.
    
    typedef int Int5Array[5];  // type is an array of 5 ints
    Int5Array ar2[5];          // an array of 5 Int5Arrays.
    

    Now the pointer arithmetic part. Just as a typed structure pointer can be advanced by the size of the structure in bytes, so can a full dimension of an array be hopped over. This is easier to understand if you think of the multi-dimensioned array as I declared ar2 above:

    int (*arptr)[5] = ar1; // first row, address of ar1[0][0].
    ++arptr;               // second row, address of ar[1][0].
    

    All of this goes away with a bare pointer:

    int *ptr = ar1; // first row, address of ar1[0][0].
    ++ptr;          // first row, address of ar1[0][1].
    

    Therefore, when doing the pointer arithmetic for two-dimensional array, the following would NOT work in getting the element at [2][2] of a multi-dimensioned array:

    #define NUMBER_OF_COLUMNS   5
    int y[5][NUMBER_OF_COLUMNS];
    int x = *(y + 2 * NUMBER_OF_COLUMNS + 2); // WRONG
    

    The reason is hopefully obvious when you remember that y is an array of arrays (declaratively speaking). The pointer arithmetic of adding the scaler (2*5 + 2) to y will add 12 rows, thereby computing and address equivalent to &(y[12]), which is clearly not right, and in fact, will either throw a fat warning at compile time or outright fail to compile altogether. This is avoided with the cast of (int*)y and the resulting type of the expression being based on an bare pointer-to-int:

    #define NUMBER_OF_COLUMNS   5
    int y[5][NUMBER_OF_COLUMNS];
    int x = *((int *)y + 2 * NUMBER_OF_COLUMNS + 2); // Right!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already know that most implementations use a single thread, but is there anything
Does anyone know if it is possible to create a single connection string that
I know that Phonegap has an event for back button, but it's only available
I know that this sort of question has been asked here before, but still
I know that Java have its own garbage collection, but sometimes I want to
I know that I can hijack a form by showing a login form in
I know that I can create an associative array like this: var MyAssocArray =
I have just started writing socket programs. Came to know that single UDP packet
I'm trying to filter a two-dimensional array with VBScript but VBScript's built-in filter function
I have a handler that handles single files (text based) perfectly. I can receive

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.