-1

here is code

#include<stdio.h>
int main(){
    int a[5] = {4,6,8,9,4};
    printf("%d",*(&a));
    return 0;    
    }

'*' operator is use for getting value at address '&' operator is use for getting address of any variable then value at(value of a) should print the value of first element of 'a' but it is printing value of first element of 'a'. can you explain me why

Geno C
  • 1,401
  • 3
  • 11
  • 26
  • Change `*(&a)` to `**(&a)` or `*a`. Remember that `a` is not an array, it is only the address of an array. – Guokas Jan 23 '21 at 07:20

2 Answers2

3

It's because a is declared as an array. In this case, a itself is the address of the array, and &a is the address of the variable that holds the address of that array.

So *a will return the first element of the array, not *(&a). Remember that your expression will be correct for non-array types, and you will get the value of the variable.

Afshin
  • 8,839
  • 1
  • 18
  • 53
2

"a" is the first element of a. But it seems like you know this.

should print the value of first element of 'a' but it is printing value of first element of 'a'

*(&a) is essentially saying value-at(address-of(a)). So you're returning a. If you were to take *(&a + 1) you would get the value of the int after the first element, AKA the second element.

If you want to get the address to print, call printf("%p", &a).

William Rosenbloom
  • 2,506
  • 1
  • 14
  • 37