metasj
metasj

Reputation: 65

Pointer to an integer array

Suppose mat is a pointer to an array of size 5 where each element is an integer

int (*mat)[5];

and I have initialized it as

int a[5] = {5, 4, 3, 2, 1};
mat = &a;

I've written the code as

#include <stdio.h>

int main()
{
    int (*mat)[5];
    int a[5] = {5, 4, 3, 2, 1};
    mat = &a;
    printf("%p\n%p\n%d\n", mat, *mat, **mat);
    return 0;
}

Output:

43800  
43800  
5

Why does mat and *mat give the same answer?

Upvotes: 0

Views: 109

Answers (3)

FatalError
FatalError

Reputation: 54541

Quite simply, the address of an array is the same as its first element's. In this context, *mat is of type int[5] which decays to int *, i.e. a pointer to the first element of the array.

Upvotes: 1

chux
chux

Reputation: 153338

mat is a pointer to array 5 of int.

*mat is an array 5 of int.

Arrays such as *mat, when pass to functions are converted in "... to an expression with type pointer to type that points to the initial element of the array object...". C11 §6.3.2.1 3

*mat in printf("%p\n",*mat); is a pointer to an int.


"Why does mat and *mat give the same answer?"

These both point to the same memory location. mat points to the array. In printf(), *mat points to the first array element, an int.

As pointers, mat and *mat point to very different types. They could have employed different presentation (even size) and yielded different text output, yet their values are equivalent. This difference is uncommon.


Further, "%p" expects a void*. Use a void* cast for portable code.

printf("%p\n%p\n",mat,*mat);  // undefined behavior
printf("%p\n%p\n",(void*) mat,(void*) *mat);

Upvotes: 0

John Bode
John Bode

Reputation: 123458

A picture may help:

     +---+            +---+
mat: |   | ------> a: | 5 | a[0]
     +---+            +---+
                      | 4 | a[1]
                      +---+
                       ...
                      +---+
                      | 1 | a[4]
                      +---+

So, first thing we notice - the address of the array a is the same as the address of the array element a[0]. The expression a has type "5-element array of int"; unless that expression is the operand of the sizeof or unary & operators, it is converted ("decays") to an expression of type "pointer to int", and the value of the expression is the address of the first element of the array. Thus, the expressions &a, a, and &a[0] will all yield the same value (address of the first element of a), even though they're not all the same type (int (*)[5], int *, int *).

So, given the above, all of the following are true:

  mat == &a 
 *mat ==  a
**mat ==  a[0] 

Since &a and a evaluate to the same address, mat and *mat evaluate to the same value.

Upvotes: 2

Related Questions