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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:29:58+00:00 2026-06-11T17:29:58+00:00

The following snippet declares a 2-D array of 4 X 10 using malloc function

  • 0

The following snippet declares a 2-D array of 4 X 10 using malloc function

/* Declare a pointer to an array that has 10
ints in each row. */
int (*p)[10];
register int i, j;
/* allocate memory to hold a 4 x 10 array */
p = malloc(40*sizeof(int));

But I do not understand how does p become a 2-D array. Initially p is declared to be an array of pointers that point to int. What happens after the call to malloc ? I am unable understand this.

  • 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-11T17:29:59+00:00Added an answer on June 11, 2026 at 5:29 pm

    In C, pointers and arrays are not the same, despite looking very similar. Here p is of type “pointer to array of 10 ints”. You’re using it as a “pointer to array of 4 arrays of 10 ints”, which is a single block of memory (the only pointer is the outermost pointer). It’s basically a dynamically allocated int[4][10].

    The trick to reading these definitions is to realize that they’re written the same way you use the item. If you have:

    *x[10];
    

    The array subscript is applied first, then the pointer dereference. So it’s an array of pointers if you define int *x[10]. If you use parenthesis to override normal precedence, you can get the pointer dereference to happen first, so you have a pointer to an array.

    Confusing? It gets worse. In function arguments, the outermost array of a function parameter is converted into a pointer.

    int *p[10]; // array of 10 pointer to int
    int (*p)[10]; // pointer to array of 10 ints
    void foo(int *p[10] /* pointer to pointer to int */);
    void foo(int (*p)[10] /* pointer to array of 10 ints */);
    

    Further, arrays are converted to pointers when you use them.

    int x[10]; // array of 10 int
    sizeof(x); // 10 * sizeof(int)
    int *y = x; // implicitly converts to a pointer to &x[0]!
    sizeof(y); // sizeof(int *)
    

    This means that you can allocate memory for an array of arrays, then let that implicitly convert to a pointer to an array, which you in turn use as if it were an array of arrays!

    Anyway, this is all very confusing so please do not ever use this in production code – at least, not without a clarifying typedef:

    typedef int vector[3];
    vector *array_of_vectors; // actually a pointer to a vector, 
                              // but you can use it as an aray to a vector if enough
                              // memory is allocated
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My IDE's tooling shows that xs has type Int* in the following snippet: def
The following snippet from RuntimeUtil.java from jlibs guarantees GC that garbage collection is done.
The following code snippet creates three arrays, which are passed into a PL/R function.
Using the following code snippet as an illustration to my question: // #includes and
Given following snippet (MS SQL): DECLARE UpdateList CURSOR FOR SELECT MyColumn FROM MyTable OPEN
In the following code snippet I've declared the variable id within the submit function.
Consider the following code-snippet typedef int type; int main() { type *type; // why
In the following code snippet I declare a global variable and then check for
I extracted the following snippet from Profiler (from a statement that fails due to
Consider the following code snippet: #include <iostream> using namespace std; struct Element { void

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.