1

I'm having trouble understanding what a specific syntax accomplishes in a piece of code. I understand what the code accomplishes overall, I'm just trying to understand exactly how it works so it's no longer just "magic".

The line I'm having trouble with is the last line, however I do understand what is happening before the = operator. The part I'm having difficulty understanding is what is occurring after the operand. More specifically this part:

L" ABCDEFG=#"[pField[y*fieldWidth + x]];

The code at a high level is meant to iterate through a 2D array, then convert it into a 1D array, checking each piece for the int value it holds, then drawing a specific symbol to that place in the array, later to be displayed in the terminal.

Not sure if this is important, but you may want the context. The array pField, is an array which holds the boarder to the actual game, it contains a 9 if the piece of the array represents a boarder, and a 0 otherwise. There is actually already a stack overflow related to the pField array, ill leave it here I'm having trouble understanding the syntax used in a piece of code

My question is, I understand what the code does at a high level, but I don't understand the syntax used to get it do what it is meant to do, L" ABCDEFG=#"[pField[y*fieldWidth + x]]; is the piece of syntax where I really just am not sure what I'm looking at, or even what this is called.

Here is the full loop and logic structure:

wchar_t *screen = new wchar_t[nScreenWidth*nScreenHeight];

for (int x = 0; x < fieldWidth; x++)

            for (int y = 0; y < fieldHeight; y++)

                screen[(y + 2)*nScreenWidth + (x + 35)] = L" ABCDEFG=#"[pField[y*fieldWidth + x]];

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
broncoian2
  • 59
  • 5
  • 3
    `L" ABCDEFG=#"[x]` gets the xth character of the string. The index x is whatever number is stored in `pField[y*fieldWidth + x]` but you don't show what is in pField so... Or are you asking why `y*fieldWidth + x` generates a 1D index from a 2D point (x,y) ? – Jerry Jeremiah Oct 07 '20 at 00:31
  • @JerryJeremiah There is actually a description and link to everything pField, already included, but I'm not asking what that does, I'm just asking about the syntax. You cleared it up perfectly – broncoian2 Oct 07 '20 at 00:39
  • 1
    Does this answer your question? [What exactly is the L prefix in C++?](https://stackoverflow.com/questions/13087219/what-exactly-is-the-l-prefix-in-c) – smac89 Oct 07 '20 at 00:40
  • This is a good example of a "cool trick" being used instead of clear and concise self-documenting code. – paddy Oct 07 '20 at 00:42
  • Thanks for the answer smac89, I was confused about that a few days ago, but actually stumbled upon the same set of answers! hahaha – broncoian2 Oct 07 '20 at 00:43
  • @paddy, That is correct, I'm following along with a video at the moment, he uses this piece without explaining it at all really outside of a high level. Just trying to dissect this. – broncoian2 Oct 07 '20 at 00:45

1 Answers1

4

L" ABCDEFG=#"[pField[y*fieldWidth + x]]; is the piece of syntax where I really just am not sure what I'm looking at, or even what this is called.

Let's break it down, so the following

L" ABCDEFG=#"

has the type

wchar_t const (&) [11]

You can check that with

static_assert(std::is_same_v<decltype(L" ABCDEFG=#"), wchar_t const (&) [11]>);

And what the code is doing is indexing into that array with the result of:

pField[y*fieldWidth + x]
Andreas DM
  • 10,685
  • 6
  • 35
  • 62