0

I'm confused about the de-referencing of pointers, if I've the below code snippet:

char str[]="ABC";
char *a=str;

Then what does a+1 refer to?

Is it B or BC as a whole?

This confusion has dawned in me because when I give:

cout<<a+1;

It gives me BC instead of B, why is it like that?

If I've a function to swap values and if it takes in arguments like:

void swap((a+1),(a+2))

How will the above function work out? Will it swap B with C?

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • It points to `B`. But `B` is followed by `C` so, it's down to how you want to visualize it for your given purpose. – Galik Feb 05 '18 at 17:54
  • Related: [With arrays, why is it the case that a\[5\] == 5\[a\]?](https://stackoverflow.com/questions/381542/with-arrays-why-is-it-the-case-that-a5-5a) –  Feb 05 '18 at 17:58
  • That's an odd swap function. You should read its documentation, or the definition to find out what it does. – eerorika Feb 05 '18 at 18:07
  • `Will it swap B with C?` Yes, it should swap B and C. Hint: could use `std::iter_swap` for it: https://ideone.com/SYAVVS – Killzone Kid Feb 05 '18 at 18:18

4 Answers4

3

Here's your pointer, a, pointing to the start of the string "ABC" (note the \0 at the end represents the implicit NUL-terminator byte):

ABC\0
^
|
a

Here's what it looks like after you add one to a:

ABC\0
 ^
 |
 a

Note that a is now pointing to the next entry in the char-array. cout will print chars until it reaches the NUL-terminator byte at the end of the array, so if you do a cout<<(a+1) it will print "BC".

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
2

The difference between pointing to an element, and pointing to the beginning of a range of elements depends entirely on how you use that pointer. It's not an intrinsic property of the pointer. Whether a+1 refers to 'B' or "BC" depends on what you do with the pointer. It just so happens that std::cout treats all char pointers as pointers to C strings.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
1

Then what does a+1 refer to?

a points to the first element of the array str. The value of the first character is 'A'. The result of a + 1 expression is a pointer to the next element of str after the one that a points to. The next element is the character with the value 'B'.

This confusion has dawned in me because when I give:

cout<<a+1;

It gives me BC instead of B, why is it like that?

This is because when you insert a pointer to a character into a character stream (such as std::cout), it is assumed to point to a character in an array (as it does in this case), and the behaviour is to stream all characters in that array until the null termination character (\0) is reached.

So, just like when you insert the pointer that points to A and all characters starting from A until the null termination character are printed (ABC), similarly when you pass the pointer that points to B then all characters starting from B are printed (BC).

You can dereference the pointer to insert just that one character, if that is your intention:

std::cout << *a;       // A
std::cout << *(a + 1); // B

or

std::cout << a[0]; // A
std::cout << a[1]; // B
Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
0
char str[]="ABC";
char *a=str;

a is char pointer and it's points to str as below

    --------------------
   |  A  |  B  C  | \0  |
    ----------------------
  0x100   0x101 ... .. (lets say 0x100  is the base address of str)
  str
  a

If you prints

cout<<a<<endl;/* it starts from 0x100 location and prints upto NULL */

and

/* a+1 == 0x100 + 1x1  == 0x101 */
cout<<a+1<<endl; /* a+1 means it starts printing from 0x101 location upto NULL character so it prints BC*/

Edit :

void swap((a+1),(a+2))

How will the above function work out? Will it swap B with C? => yes it will swap B and C, depends upon swap function logic.

Achal
  • 11,821
  • 2
  • 15
  • 37