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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:27:21+00:00 2026-05-13T16:27:21+00:00

When working with arrays and pointers in C, one quickly discovers that they are

  • 0

When working with arrays and pointers in C, one quickly discovers that they are by no means equivalent although it might seem so at a first glance. I know about the differences in L-values and R-values. Still, recently I tried to find out the type of a pointer that I could use in conjunction with a two-dimensional array, i.e.

int foo[2][3];
int (*a)[3] = foo;

However, I just can’t find out how the compiler “understands” the type definition of a in spite of the regular operator precedence rules for * and []. If instead I were to use a typedef, the problem becomes significantly simpler:

int foo[2][3];
typedef int my_t[3];
my_t *a = foo;

At the bottom line, can someone answer me the questions as to how the term int (*a)[3] is read by the compiler? int a[3] is simple, int *a[3] is simple as well. But then, why is it not int *(a[3])?

EDIT: Of course, instead of “typecast” I meant “typedef” (it was just a typo).

  • 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-13T16:27:22+00:00Added an answer on May 13, 2026 at 4:27 pm

    First, you mean "typedef" not "typecast" in your question.

    In C, a pointer to type T can point to an object of type T:

    int *pi;
    int i;
    pi = &i;
    

    The above is simple to understand. Now, let’s make it a bit more complex. You seem to know the difference between arrays and pointers (i.e., you know that arrays are not pointers, they behave like them sometimes though). So, you should be able to understand:

    int a[3];
    int *pa = a;
    

    But for completeness’ sake: in the assignment, the name a is equivalent to &a[0], i.e., a pointer to the first element of the array a. If you are not sure about how and why this works, there are many answers explaining exactly when the name of an array "decays" to a pointer and when it does not:

    • My answer to a question titled type of an array,
    • Another answer with examples of instances when the name of an array does not decay to a pointer, and
    • The answers to what is array decaying.

    I am sure there are many more such questions and answers on SO, I just mentioned some that I found from a search.

    Back to the topic: when we have:

    int foo[2][3];
    

    foo is of type "array [2] of array [3] of int". This means that foo[0] is an array of 3 ints, and foo[1] is an array of 3 ints.

    Now let’s say we want to declare a pointer, and we want to assign that to foo[0]. That is, we want to do:

    /* declare p somehow */
    p = foo[0];
    

    The above is no different in form to the int *pa = a; line, because the types of a and of foo[0] are the same. So, we need int *p; as our declaration of p.

    Now, the main thing to remember about arrays is that "the rule" about array’s name decaying to a pointer to its first element applies only once. If you have an array of an array, then in value contexts, the name of the array will not decay to the type "pointer to pointer", but rather to "pointer to array". Going back to foo:

    /* What should be the type of q? */
    q = foo;
    

    The name foo above is a pointer to the first element of foo, i.e., we can write the above as:

    q = &foo[0];
    

    The type of foo[0] is "array [3] of int". So we need q to be a pointer to an "array [3] of int":

    int (*q)[3];
    

    The parentheses around q are needed because [] binds more tightly than * in C, so int *q[3] declares q as an array of pointers, and we want a pointer to an array. int *(q[3]) is, from above, equivalent to int *q[3], i.e., an array of 3 pointers to int.

    Hope that helps. You should also read C for smarties: arrays and pointers for a really good tutorial on this topic.

    About reading declarations in general: you read them "inside-out", starting with the name of the "variable" (if there is one). You go left as much as possible unless there is a [] to the immediate right, and you always honor parentheses. cdecl should be able to help you to an extent:

    $ cdecl
    cdecl> declare p as  pointer to array 3 of int
    int (*p)[3]
    cdecl> explain int (*p)[3]
    declare p as pointer to array 3 of int
    

    To read

    int (*a)[3];
    
          a            # "a is"
        (* )           # parentheses, so precedence changes.
                       # "a pointer to"
            [3]        # "an array [3] of"
    int        ;       # "int".
    

    For

    int *a[3];
    
         a             # "a is"
          [3]          # "an array [3] of"
        *              # can't go right, so go left.
                       # "pointer to"
    int      ;         # "int".
    

    For

    char *(*(*a[])())()
    
              a         # "a is"
               []       # "an array of"
             *          # "pointer to"
            (    )()    # "function taking unspecified number of parameters"
          (*        )   # "and returning a pointer to"
                     () # "function"
    char *              # "returning pointer to char"
    

    (Example from c-faq question 1.21. In practice, if you are reading such a complicated declaration, there is something seriously wrong with the code!)

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

Sidebar

Ask A Question

Stats

  • Questions 380k
  • Answers 380k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use STS (spring tool suite) a new spring… May 14, 2026 at 9:59 pm
  • Editorial Team
    Editorial Team added an answer Take a look at some of the Graphics and Animation… May 14, 2026 at 9:59 pm
  • Editorial Team
    Editorial Team added an answer If you're writing to the disk, a user can find… May 14, 2026 at 9:59 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.