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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:20:25+00:00 2026-05-21T07:20:25+00:00

a[1][2] is expanded by compiler like this: *( *(a+1) +2 ) . So if

  • 0

a[1][2] is expanded by compiler like this: *( *(a+1) +2 ). So if a has such a prototype:int **a,

The foregoing expression should be explained like this:

  1. Get the address of a from symbol table. Note it is a pointer
    to a pointer

  2. Now we add it by 1, then it point to the somewhere next to
    where a point to.

  3. Then we dereference it. I think here is a undefined behavior,
    for we don’t know if a+1 is valid and we arbitraryly access it.

  4. Ok, if we are lucky enough that we successfully get the value
    *(a+1). We add this by 2.

  5. Upon this step, we dereference (*(a+1) +2 ). Will we be lucky now?

I read this in Expert C Programming in Chapter 10. Is this correct?

  • 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-21T07:20:26+00:00Added an answer on May 21, 2026 at 7:20 am

    New answer, after edited question:

    For a[1][2] to be valid, given that a has is defined as int **a;, both of these must be true:

    • a must point at the first of two sequential int * objects;
    • The second of those int * objects must point at the first of three sequential int objects.

    The simplest way to arrange this is:

    int x[3];
    int *y[2] = { 0, x };
    int **a = y;
    

    Original answer:

    If the expression a[1][2] is valid, then there are many distinct possibilities for the type of a (even neglecting qualifiers like const):

    1. type **a; (pointer to pointer to type)
    2. type *a[n]; (array of n pointers to type)
    3. type (*a)[n]; (pointer to array of n type)
    4. type a[m][n]; (array of m arrays of n type)

    Precisely how the expression is evaluated depends on which of these types a actually has.

    First a + 1 is calculated. If a is itself a pointer (either case 1 or case 3), then the value of a is directly loaded. If a is an array (case 2 or case 4), then the address of the first element of a is loaded (which is identical to the address of a itself).

    This pointer is now offset by 1 object of the type that it points to. In case 1 and case 2, it would be offset by 1 “pointer to type” object; in case 3 and case 4, it would be offset by 1 “array of n type” object, which is the same as ofsetting by n type objects.

    The calculated (offset) pointer is now dereferenced. In cases 1 and 2, the result has type “pointer to type“, in cases 3 and 4 the result has type “array of n type“.

    Next *(a + 1) + 2 is calculated. As in the first case, if *(a + 1) is a pointer, then the value is used directly (this time, cases 1 and 2). If *(a + 1) is an array (cases 3 and 4), then the address of the first element of that array is taken.

    The resulting pointer (which, at this point, always has type “pointer to type“) is now offset by 2 type objects. The final offset pointer is now dereferenced, and the type object is retrieved.

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

Sidebar

Related Questions

The expanded_status_bar layout in AOSP 4.0.4 has a TextView that looks like this in
This recursive definition of a macro does what it should (sum integers from 1
What does a statement such as this mean ? int x = ( {
Today I stumbled upon a rather interesting compiler error: int main() { int const
Given this definition: class Foo(var x: String) {} object Helper { def model[T](get: ⇒
What is wrong with this code? object Numbers extends App { def decode(number: Int)
Somehow I don't get how variadic template parameter packs are expanded. What's wrong with
I learned that compiler will expand macros while compiling. Templates are also expanded at
(I feel like this must be a duplicate, but I can't find it). Consider:
I have this makefile: SHELL=/bin/zsh COMPILER=g++ -g COMPILERSO=g++ -a +a1 -b LIBDIR=/Users/romeovs/Desktop/rootex CFLAGS=$(root-config --cflags)

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.