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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:16:11+00:00 2026-06-15T08:16:11+00:00

I yesterday came across a question on SO, that wanted to dynamically allocate a

  • 0

I yesterday came across a question on SO, that wanted to dynamically allocate a 2-D array in C.

One of the answers was to allocate it this way:

int (*place)[columns] = malloc(rows * sizeof *place);

This apart from being beautiful, brought a question to my head. The question goes below:

Following is the code in which i allocate a 4×4

    int (*arr)[4] = (int (*)[4]) malloc(4 * sizeof *arr);
    printf("%d\n", sizeof arr);                 //Dynamic 2-D array by above method
    
    int **arr1 = (int**) malloc(4 * sizeof(int*));
    for(int i = 0; i < 4; i++)
            arr1[i] = (int *) malloc(sizeof(double));
    printf("%d\n", sizeof arr1);                          //Usual dynamic 2-D array

    int *arr2 = (int*) malloc(4 * sizeof(int));
    printf("%d\n", sizeof arr2);                          //Dynamic 1-D array
    
    

The usual output is:

4
4
4

However, if i try to print sizeof *arr, sizeof *arr1 and sizeof *arr2, the output is:

16
4
4

I don’t understand why this is happening. Any idea why the output for sizeof *arr is 16? How is the memory being being allocated in the first case?

Also, when i try to print the address of arr and *arr, both the printed values are same. *arr means "value at" arr. So does that mean arr stores its own address, i.e., it is pointing to itself (which i don’t think is possible)? Am slightly confused. Any idea where am I going wrong?

Thanks for your help!

  • 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-15T08:16:12+00:00Added an answer on June 15, 2026 at 8:16 am

    However, if I try to print sizeof *arr, sizeof *arr1 and sizeof *arr2, I get 16, 4, and 4.

    where:

    int (*arr)[4];
    int **arr1;
    int *arr2;
    

    You must be working on a 32-bit machine, not a 64-bit machine.

    The type of arr is ‘pointer to an array of 4 int‘. When you dereference it, you get an ‘array of 4 int‘. When passed to sizeof, the array is of size 16 (4 * sizeof(int)). Most of the time, when you reference an array, the type is adjusted to ‘pointer to zeroth element of the array’, but sizeof() is the primary exception to that rule; it sees an array as an array and returns the size of the whole array.

    The type of arr1 is ‘pointer to pointer to int‘. When you dereference it, you get a ‘pointer to int‘. When passed to sizeof, the pointer is of size 4 (sizeof(int *)).

    The type of arr2 is ‘pointer to int‘. When you dereference it, you get an int. When passed to sizeof, the int is of size 4.


    Okay! I get most of it. But … why isn’t the output 16 when I do sizeof arr2?

    As James noted in his comment:

    because sizeof(arr2) reduces to sizeof(int*) which on a 32-bit machine is 4.

    That is, arr2 is a pointer; the size of a pointer is 4 on a 32-bit machine.

    Also, arr is a ‘pointer to an array of 4 int’, i.e., a single pointer. Then how do I get a 4×4 matrix.

    You can use either of these:

    int mat1[4][4];
    int (*mat2)[4][4];
    

    The first is a straight-forward 4×4 matrix. The result of sizeof(mat1) will be 64 on a 32-bit machine (and most 64-bit machines, as it happens).

    The second is a pointer to a 4×4 matrix of int. The result of sizeof(mat2) will be 4 on a 32-bit machine; mat2 is a pointer (to a 4×4 matrix of int), so it is of size 4, the same as every other object pointer (and, in practice, the same size as every function pointer, though the C standard does not guarantee that function pointers and object pointers are the same size; POSIX does guarantee that, though).

    The result of sizeof(*mat2) is the size of the object that mat2 points at, which is a 4×4 matrix of int, so the size is 64 again.

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

Sidebar

Related Questions

Came across an interesting little problem at work yesterday. This is a question about
Yesterday I read some code of a colleague and came across this: class a_class
Yesterday I came across Craig Reynolds' Boids , and subsequently figured that I'd give
I started reading about struts2 yesterday and came across the below slide. Site link
I came across http://www.damnlag.com/ yesterday and I have no idea how they have managed
While teaching my JavaScript class yesterday, my students and I came across some interesting
I came across a problem yesterday where my program failed as 32 bit process.
I came across Google's Page Speed add-on for Firebug yesterday. The page about using
I tried the recently released generators yesterday but I came across a problem. I
I asked a question yesterday on how to pull out multiple results into one

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.