3

in the following code :

int main()
{

    int i = 15, j = 1;
    int *a[] = {&i, &j};
    printf("%d", (*a)[0]);
    return 0;

}

the output of (*a)[0] is 15 (value of i) however when I tried to check (*a)[1] it gives a garbage value. I would expect that the same expression should work for all the entries in the array however it works only for the first element in the array.

2
  • 2
    *a is &i , (*a)[0] is *(&i + 0), (*a)[1] is *(&i + 1) it's UB.
    – BLUEPIXY
    Jan 31, 2014 at 13:12
  • 1
    (*a) gives you the first element of the array (same as a[0]) the following [0] gives the int at that address. While (*a)[1] is the int at the address after the address of i
    – Micka
    Jan 31, 2014 at 13:21

4 Answers 4

8

Here you have declared array of pointers. At first you should know the various representation of array. How a[i] is calculate? a gives you the base address of array.

a[ i ]  = *(a+i)
(*a)[0] = *(*a+0)
(*a)[1] = *(*a+1)

Thus the value of (*a)[0] = *(address of i + 0) = Value of i  
And the value of  (*a)[1] = *(address of i + 1) = value at the location of (add of i + 2) i.e. a garbage value.

In above problem you can get the value correctly if you use *( a[ 0 ] ) and *( a[1] ).

2
  • How do you figure *(address of i + 1) is the “value at the location of (add of i + 2)”? Where does the 2 come from? Are you assuming the size of an int is 2, so you mean 2 bytes? Jan 31, 2014 at 14:32
  • @EricPostpischil, yes i am assuming the sizeof(int) = 2. That's why i have added 2.
    – Rahul
    Feb 3, 2014 at 5:49
3

You shouldn't try (*a)[0] and (*a)[1] in the first place. You should try *(a[0]) and *(a[1]).

You have an array of int *'s called a. a has two elements, a[0] and a[1]. a[0] is &i and a[1] is &j. Therefore *(a[0]) is i and *(a[1]) is j.

7
  • 2
    This does not explain why, which is the main question. Jan 31, 2014 at 13:12
  • No need to use complex syntax. *a[0] is enough.
    – user694733
    Jan 31, 2014 at 13:13
  • @RubenSerrate It has unnecessary noise which makes code harder to read.
    – user694733
    Jan 31, 2014 at 13:15
  • @ShafikYaghmour Well, if a is not a pointer, starting by (*a) and then accessing (*a)[1] does not seem to make much sense to me. Doing it the right way, you avoid facing the problems which the OP observes by doing it the wrong way. Jan 31, 2014 at 13:17
  • 2
    Maybe once you're used to it, but *(a[0]) helps you understand the concept better, I think. Jan 31, 2014 at 13:18
2

I think you are confused over the semantics of pointers here (lots of people are)

int x[2]; // an array of two ints
int *y[2]; // an array of two int pointers.

typeof(x[0]) == int // this isn't really valid C code
typeof(*x)   == int // arrays are (pretty much) pointers

typeof(y[0]) == int*
typeof(*y)   == int*
typeof(**y)  == int
3
  • Arrays are not pointers. They will decay into pointers in some circumstances.
    – user694733
    Jan 31, 2014 at 13:27
  • You can treat them like pointers most of the time, the only time they don't really function like pointers is trying to reassign them.
    – Michael
    Jan 31, 2014 at 13:34
  • ...and the way sizeof works on them, and the way that memory is allocated for them. Pointers are always about memory address being stored, array are always about data being stored. There is not anything in common with them apart from some syntatic sugar.
    – user694733
    Jan 31, 2014 at 13:38
1

try this one below, you will better understand whats going on, this is not an answer to your question but should help you understand.

oops, you will have to assign the array name to a pointer, you wont be allowed to increment the array name by this ++ since its not a variable,

 int **s = a;
(*++s)[0] this will print 1.

since the array name points to the first element, this will increment it by 1(now it will point to the second element), then dereference it, and take the [0] element of the memory pointed to by (*++a)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.