-11
#include <bits/stdc++.h>

using namespace std;
struct {
  struct {
    struct {
      char *OwO[12];
    }iwi;
  }uwu;
}owo;

int main() {
  *owo.uwu.iwi.OwO = "What's this?";
  printf("%s\n", *owo.uwu.iwi.OwO);
  return 0;
}

Hi guys I don't know how this code actually work? Can anyone explain this to me?

J. Antonio Perez
  • 8,918
  • 16
  • 37
  • 2
    That is C++, but you have tagged this question with C. If you thought the code was C, then the answer is it is C++. If you thought it was C++ and want an explanation of what it means in C++, then you have tagged the question incorrectly. In either case, what specific part(s) do you not understand? Do you understand the `#include`? Do you understand the `using`? Do you understand the `struct` declarations? Do you understand the assignment in the first line after `main`? Do you understand the `printf` statement? – Eric Postpischil Aug 06 '19 at 18:32
  • You should *never* `#include `. It is not proper C++. It ruins portability and fosters terrible habits. See [Why should I not `#include `](https://stackoverflow.com/q/31816095). – L. F. Aug 07 '19 at 06:45
  • Also, please avoid `using namespace std;`. It is considered bad practice and will ruin your life. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) – L. F. Aug 07 '19 at 06:45

2 Answers2

2

Consider for example this declaration

struct {
  char *OwO[12];
}iwi;

It at first declares an unnamed structure with one data member that has the type of an array with 12 elements of the type char *. And then it declares an object named iwi of the structure.

So to access the data member OwO of the object iwi you can use the expression

iwi.OwO

that returns lvalue of the array OwO.

If to apply the operator * to the expression then the array OwO is implicitly converted to pointer to its first element and has the type char **. Dereferencing the pointer we get the first element of the array of the type char *.

We can assign the element with a string literal as

*iwi.OwO = "What's this?";

That is the first element of the array that has the type char * now gets the address of the string literal.

Here is a demonstrative program

#include <stdio.h>

struct {
  char *OwO[12];
} iwi;

int main(void) 
{
    *iwi.OwO = "What's this?";

    printf( "%s\n", *iwi.OwO );

    return 0;
}

Its output is

What's this?

In the original code this unnamed structure is included into two other unnamed structures

struct {
  struct {
    struct {
      char *OwO[12];
    }iwi;
  }uwu;
}owo;

That is with have object owo of the unnamed outer structure that has data member uwu of the enclosed unnamed data structure that in turn has data member iwi of the most inner unnamed structure.

So to access the data member OwO we have to list all names of object

owo.uwu.iwi.OwO

So we have gotten an access to the most inner data member OwO. And now dereferncing the expression as it was shown in the demonstrative program above we initialize the first element of the array with the string literal "What's this?".

And in the same way we can output it using this full expression

printf("%s\n", *owo.uwu.iwi.OwO);
Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
1

It's several nested unnamed struct types.

Here is the same thing using named types, and indexing instead of dereferencing.

struct Inner
{
    char* OwO[12];
};

struct Middle
{
    Inner iwi;
};

struct Outer
{
    Middle uwu;
};

Outer owo;

int main() {
  owo.uwu.iwi.OwO[0] = "What's this?";
  printf("%s\n", owo.uwu.iwi.OwO[0]);
  return 0;
}
molbdnilo
  • 55,783
  • 3
  • 31
  • 71