-1

Suppose I have this code section

char *ptr[] = {"Hi", "Alexa"};
  std::cout<<++*ptr<<std::endl;
  std::cout<<++*ptr<<std::endl;
  std::cout<<++*ptr<<std::endl;
  std::cout<<++*ptr<<std::endl;

The out put is

i

Alexa
lexa

Why is there a space between "i" and "Alexa"? And if I change" endl" to "\n", there won't be any space.

supnep
  • 45
  • 6
  • 4
    You have out of bound access hense UB. It is quite pointless to discuss your code behaviour - it is undefined. – Slava Aug 25 '22 at 15:54
  • Wow, I would not have guessed that `++*ptr` would increment the pointer and not the character. – Mark Ransom Aug 25 '22 at 15:59
  • 1
    I'm not convinced you understand what `++*ptr` is actually doing. It would improve you question *tremendously* if you noted why you're doing that in your code, and your expectations from said-same. Also, that code should flag at-least warnings with any decent C++ compiler, as both of those literals are read-only, and you setting up an array of non-const hot pointers to them. – WhozCraig Aug 25 '22 at 16:00
  • 4
    @MarkRansom array of pointers. `*ptr` = first pointer, not first character of first pointer. – WhozCraig Aug 25 '22 at 16:01
  • @WhozCraig yeah, I just figured that out too. Makes this question even crazier. – Mark Ransom Aug 25 '22 at 16:02
  • @WhozCraig I'm just trying to understand how the operator ++ works with a pointer. Suppose I want to move to the next element of the array, then how can the operator help me to do it? I'm using an online compiler on my phone right now so I suppose It could cause things that normal compilers wouldn't – supnep Aug 25 '22 at 16:18
  • 1
    "I'm just trying to understand how the operator ++ works" running random code is not the best way to understand how things work, especially in C++. Very often you will learn how things do not work, but you have no way to know they actually don't. – Slava Aug 25 '22 at 16:21

1 Answers1

3

"" ({'\0'}) is valid string, an empty string.

the Alexa is printed probably because compiler put it just after "hi", but it's undefined behavior.

possible memory layout : { h, i, \0, A, l, e, x, a, \0 }


i     // "hi"+1 -> {'i','\0'}
      // "hi"+2 -> {'\0'}, empty string
Alexa // UB, just incidentally it's place there by compiler
lexa  // UB, just incidentally it's place there by compiler
apple apple
  • 10,292
  • 2
  • 16
  • 36
  • Where did the `i` go to or come from in the first line of your code snippet? And, now (third or fourth edit?) where's the backslash in the first line of your post. – Adrian Mole Aug 25 '22 at 16:00
  • @AdrianMole `*ptr` is a pointer to "hi" (char*) so increase it point to "i" (although it should be `const char*`) – apple apple Aug 25 '22 at 16:02
  • But you have `{'h','\0'}` ... shouldn't that be `{'i','\0'}` ? – Adrian Mole Aug 25 '22 at 16:02
  • @AdrianMole fixed, it should be `i` :P – apple apple Aug 25 '22 at 16:03
  • That's the trouble with posting fast answers ... mistakes happen and the question gets closed while you're trying to fix them. – Adrian Mole Aug 25 '22 at 16:04
  • @AdrianMole edits to an answer are still allowed even if the question is closed. – Mark Ransom Aug 25 '22 at 16:05
  • @AdrianMole I'm not sure what you mean. a typo happened whether I type it fast or slow. And I think the intent is pretty clear as very first sentence. (btw the dupes doesn't show why there is empty line, which is no UB (at it's own) ) – apple apple Aug 25 '22 at 16:06
  • @Mark Indeed. That doesn't mean the question is not a duplicate, though. However, *maybe* it isn't actually a duplicate, because the space/blank line happens before the UB. – Adrian Mole Aug 25 '22 at 16:06
  • That memory layout is probably not accidental, but you're right in calling out UB. – Mark Ransom Aug 25 '22 at 16:06
  • @AdrianMole unfortunately SO is influencing fast answers. I've seen many examples people post placeholder and then edit it to real answer. – Slava Aug 25 '22 at 16:07
  • 1
    @Slava Yes, I know. In this case, though the edit history doesn't show it, there were lots of separate edits (maybe 6 or 7) - mostly made within the "grace period" of each other. – Adrian Mole Aug 25 '22 at 16:08
  • And why change `accident` (correct) to `incident` (not correct)? – Adrian Mole Aug 25 '22 at 16:10
  • 1
    @AdrianMole the undefined behavior isn't restricted to after the code that triggers it. One of my favorite posts on UB: https://devblogs.microsoft.com/oldnewthing/20140627-00/?p=633 – Mark Ransom Aug 25 '22 at 16:11
  • @MarkRansom incident, not very good at english :) If there is other function using the string, compiler would probably put them away. – apple apple Aug 25 '22 at 16:12
  • @AdrianMole not good at English, I'd like to say not deterministic and no harm, is there a good way? – apple apple Aug 25 '22 at 16:13
  • 1
    OK - You can use "by accident" or "incidentally" I suppose. But not "by incident". – Adrian Mole Aug 25 '22 at 16:15