1
int main() {
    struct person *p;
    p = NULL;
}

I'm referring to this structured person but I did not define it anywhere, so i would expect that this would give some type definition error. does it compile? it seems it compiles. "cpp is not strictly typed language. so other languages may not be compiled but cpp does. Why this happens? Thanks for help and comments.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 4
    You implicitly defined the name with: `struct person`. And all pointers, even of *unknown* types can have `NULL`, `0` or `nullptr` assigned to them. You would not get away with `person* p;` – quamrana Apr 24 '21 at 16:58
  • Read some C++ standard like [n3337](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf) or better, and read [a good C++ programming book](https://www.stroustrup.com/programming.html) – Basile Starynkevitch Apr 24 '21 at 17:18
  • Study for inspiration the source code of existing open source projects in C++ like [FLTK](http://fltk.org/), [fish](http://fishshell.com/), [RefPerSys](http://refpersys.org/). For RefPerSys, contact me by email at `basile@starynkevitch.net` – Basile Starynkevitch Apr 24 '21 at 17:35

1 Answers1

4

It's called an Elaborated type specifier. Elaborated type specifiers also forward declare the class. So, it's valid to create a pointer to that type you have not defined yet, but you can't dereference the pointer or instantiate the class since it's an incomplete type.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93