Dark Innocence
Dark Innocence

Reputation: 1389

How come does the size of arr[0] is of 8 bytes?

I was browsing for finding a way to find number of rows and columns in of a given matrix without any additional information and I came around this answer.

Given a matrix, find number of rows and columns

This is the code snippet from the second answer of the question above :

int main()
{
    float a[9][2]={{0,1},{1,1}};
    int row=(sizeof(a)/sizeof(a[0]));
    int col=(sizeof(a)/sizeof(a[0][0]))/row;
    printf("%d\n",row);
    printf("%d\n",col);
    return 0;
}

How does the sizeof(a[0]) turns out to be 8? In my understanding the pointers have a generic size of 4 bytes in 32 bit arch and 8 bytes in 64 bit arch. I printed out the sizeof(a[0]) on both the machines and the answer is still 8. Anybody has any clue why?

Edited : I mean to say doesn't a[0] decay into a pointer?

Upvotes: 4

Views: 1218

Answers (4)

NathanOliver
NathanOliver

Reputation: 180710

a is a two dimensional array. The first dimension is the number of total arrays you have and the second number is the the number of element in each array. So float a[9][2] means you have 9 arrays all right next to each other(arrays are contiguous) and each array has two elements.

Now if we look at the sizeof operator we see that it is defined as

When applied to an array, the result is the total number of bytes in the array. This implies that the size of an array of n elements is n times the size of an element.

for arrays.

So now that we know that sizeof gives the total size of the array if given an array and we know that a[n] represents on of the two element arrays in a then sizeof(a[0]) is the total size of one of the first array(and all of them since the array is not jagged) which happens to be 8 since sizeof(float) is 4.

Upvotes: 1

Lundin
Lundin

Reputation: 213989

There are no pointers used anywhere in the code posted. Arrays do not decay to a pointer-to-first-element when passed as operand to sizeof (1).

  • a is a 2D array, of type float [9][2].
  • a[0] is the first item in this 2D array, which is a 1D array of type float [2].
  • sizeof(a) gives the size of the 2D array, equivalent to sizeof(float) * 9 * 2.
  • sizeof(a[0]) gives the size of the 1D array, equivalent to sizeof(float) * 2.
  • float is apparently 4 bytes on your system, 4 * 2 = 8.

(1) See the specification of the sizeof operator in ISO 9899:2011 6.5.3.4:

When applied to an operand that has array type, the result is the total number of bytes in the array.

The rule of "array decay" is found in 6.3.2.1/3:

Except when it is the operand of the sizeof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.

Upvotes: 11

P.P
P.P

Reputation: 121397

a[0] is an array of 2 floats (float[2]). Hence, sizeof(a[0]) is equivalent to sizeof(float[2]) in your code. On your platform sizeof(float) is 4 and hence sizeof(a[0]) is 8.

But the size of float is irrelevant forthe purpose of calculating the row and column as you do. The code you have is always going to give correct row and column values irrespective of sizeof(float).

Upvotes: 3

Rishikesh Raje
Rishikesh Raje

Reputation: 8614

But a[0] is an address right ?

No, a[0] is a 1 Dimensional array. It decays to a pointer in almost all cases. However in the case of a sizeof operation, you will get different answers for both.

As a[0] is a 1D array of 2 floats, sizeof(a[0]) gives 8.

Upvotes: 5

Related Questions