0

I have been learning about using pointers with arrays. If I was to write this simple code:

string Array[3] = {"one", "two", "three"};

string *pArray = Array;

pArray = pArray + 1;

std::cout << *pArray << std::endl;

The output is 'two'

Can someone explain to me the workings of this? Why does the +1 change the position of the pointer to where "two" is in the array? I'd expect the +1 to be concatenated onto the end of the string pointer so I'd end up with something like '0x61feb01'.

How does the compiler know to increment the array and not just add a 1 on to the end of the pointer string memory location?

Why is adding an int to a string different here?

Thanks.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Danny Hepple
  • 669
  • 1
  • 7
  • 14
  • 6
    the `+ 1` does not increment the array by any means. Learn about *pointer arithmetic* – Fureeish Jan 17 '18 at 23:00
  • 7
    Lookup _pointer arithmetics_! The compiler is aware of the data size using a typed pointer. –  Jan 17 '18 at 23:00
  • Consider renaming your variable `pArray` to `pointer`. The name seems to be confusing you into thinking that is is an array. – Drew Dormann Jan 17 '18 at 23:06
  • You're not adding on to a string, you're adding to a pointer to a string. Maybe it'd be clearer if you wrote `string* pArray = Array;`. – David Schwartz Jan 17 '18 at 23:21
  • Seems like this question has already been answered. I can't believe I missed the question titled "Why a pointer + 1 add 4 actually". I think someone should change the title of that question because it makes no sense at all in the context of what he wants to know. Am I okay looking at the answers for this even though the question is about C , is the pointer information transferable to c++? – Danny Hepple Jan 17 '18 at 23:32

1 Answers1

1

I think when you add "1" to your pointer, this "1" is not treated as an integer number in the process of addition. Notice that pointer is an address, now, when you add 1 to your pointer it becomes: the address it currently is+ONE COMPLETE SIZE of a string type. So for example if your string type takes 8 Bytes you're moving 8 Bytes forward in the memory and not 1 Bytes!

As to why "two" printed you should know that array elements are held in continuous groups of bytes in the memory thus when you add to the address of one of the elements you can get to the other elements. Array names are nothing but pointers themselves except that they are fixed pointers(you can't change their addresses). E.g. In (int myArr[10]) the name "myArr" is a pointer that points to the(has the address of the) first part in the ram that holds the first element and then using pointer arithmetic just as you've done in the above example of yours you can access the rest of the elements either.

As an ending note, these two are equal in this example array: (myArr[i]==*(myArr+i)), if you put 0 here instead of i, you get (myArr[0]==*myArr) which is exactly what I said earlier. I hope it helps.

Simon.B
  • 70
  • 1
  • 8
  • Fantastic! Perfect and concise answer. You just saved me from reading through a few pages of C pointer answers in the linked post. Cheers. – Danny Hepple Jan 17 '18 at 23:41