mercredi 4 octobre 2017

Why does this print 5?

Suppose we have the following:

int array[][2]{1, 2, 3, 4, 5, 6};

cout << ( * ( array + 1 ) )[2] << endl;

I do not understand why this prints 5.

I think that (array + 1) gives us an int *[2] to {3, 4}, so, what we can think of as the second row. Let's call this pointer ptr.

(* ptr)[2]

Is this saying that I should dereference ptr and then print whatever is at index 2? Doesn't dereferencing ptr give me an int * that points to number 3?

Aucun commentaire:

Enregistrer un commentaire