0

I learnt that just the array name by itself has the value of the address of the first element of that array. Example, int num[] ={1,2,3}; so num has the value of &num[0] and a separate address. However, when I try finding the address for this num, it gives me the same address as &num[0]. Aren't they supposed to have different addresses num and &num[0].

int main(){
    int num[4]={1,2,3,4};
    int* int_ptr = num;
    printf("%d\n", &num[0]);
    printf("%d\n", num);

    printf("%d\n", &num);
    printf("%d", int_ptr);
}

My output is:

6422268
6422268
6422268
6422268
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Bob Jane
  • 57
  • 6
  • 3
    use `%p` to print pointer values. And `num` is `&num[0]` all right. the address of the array is the address of it's first element. also works for pointers. But `&num == num` for arrays only. – Jean-François Fabre Jul 15 '17 at 16:32
  • 2
    "I learnt that just the array name by itself points to the first element of that array" - You learned wrong. An array is not a pointer! – too honest for this site Jul 15 '17 at 16:37
  • @Olaf oh my bad. I meant that it contains the address of the first element of that array. – Bob Jane Jul 15 '17 at 16:40
  • @Olaf ! He doesn't say an array were a pointer! – alk Jul 15 '17 at 16:41
  • @Jean-FrançoisFabre "the address of the array is the address of it's first element". so num (by itself) would have the same address as num[0]? – Bob Jane Jul 15 '17 at 16:41
  • The formal wording is that the array in many cases decays to the address of its 1st element. – alk Jul 15 '17 at 16:42
  • 1
    "*so num (by itself) would have the same address as num[0]*" yes. See also here: https://stackoverflow.com/q/2528318/694576 – alk Jul 15 '17 at 16:44
  • 2
    The address value of `&num` is the same as `num` and `&num[0]`, but the type is different. The type of `&num` is `int (*)[4]`; the type of `num` and `&num[0]` in a general expression is `int *`. You can spot the difference by adding 1 to the values. `num+1` and `&num[0] + 1` would be 6422272, assuming `sizeof(int) == 4`, but `&num + 1` would be 6422284 according to the values printed in the question. – Jonathan Leffler Jul 15 '17 at 16:47
  • @Jonathan Leffler - question is not about the type, only about the address. This is an interesting question as it shows the the difference between virtual calculative pointer and the real one which resides somewhere in the memory and and points to another point in the memory. – 0___________ Jul 15 '17 at 17:14
  • @PeterJ: I've not come across the term 'virtual calculative pointer' before. I'm not sure what distinction you are making between that and the 'real one' (meaning 'real calculative pointer'?). – Jonathan Leffler Jul 15 '17 at 17:17
  • I did not use this term _'real calculative pointer'_ so please do not use it. In the OP example num does not exist as a pointer in the memory. and it is an unmodifiable lvalue. So you can't do `int num[10]; int *numptr; numptr = malloc(100 * sizeof int); num = numptr;` Both num and numptr are lvalues, point to somethere, but numptr physically exist, num does not, exist only pointed values by num. – 0___________ Jul 15 '17 at 17:23
  • @alk: The cited line exactly says it The array name **is** the array. – too honest for this site Jul 15 '17 at 17:29

3 Answers3

5

Imagine a queue of people and someone pointing to the first person in the queue (that's your "address of first element"). Now imagine another person pointing to the whole queue. Do these two people point to the same direction?

What if you tell them to point to the next element (the 2nd person in the queue for the first, a different queue for the other)?

See the difference?

int arr[1000];
&arr[0] + 1 == &arr[1]
&arr + 1 == <pointer to inexistent array[1000] of int>
pmg
  • 106,608
  • 13
  • 126
  • 198
0

No becacuse num does not exist as a pointer located somewhere in the memory, and it is only an address of the continuous piece of memory to accommodate table elements. It is only something which is calculated but does not exist as a separate "physical" pointer object. so num , &num and &num[0] addresses are exactly the same and point to the beginning of the memory where table elements are stored.

Try to analyse this example:

int main()
{

    int num[10];
    int *numptr = num;

    printf("num = %p, &num = %p, &num[0] = %p\n\r", num, &num, &num[0]);
    printf("numptr = %p, &numptr = %p, &numptr[0] = %p\n\r", numptr, &numptr, &numptr[0]);

    return 0;
}

The result:

num = 0000001F3C2FFAB0, &num = 0000001F3C2FFAB0, &num[0] = 0000001F3C2FFAB0
numptr = 0000001F3C2FFAB0, &numptr = 0000001F3C2FFAD8, &numptr[0] = 0000001F3C2FFAB0
0___________
  • 60,014
  • 4
  • 34
  • 74
-1

num points to the first item in the array, same as &num[0]. that's why they both give you the same address. also use %p to print an address

c360
  • 67
  • 8
  • that's not the question. – Jean-François Fabre Jul 15 '17 at 16:35
  • you asked "Aren't they supposed to have different addresses num and &num[0]." the answer is that they both point to the same address in the memory. so no, they aren't suppose to have different addresses (if by having you mean pointing to). you are not being very clear with your question – c360 Jul 15 '17 at 16:41