-3

Why declaring a structure with its own identifier as an element of it is not allowed? where as a self referential structure which is declared by the same variable of the struct with a pointer symbol!

this is the code I tried out:

 #include<stdio.h>
    #include<conio.h>
    struct am
    {
     int a;
     struct am k; // this reported an error
    };

This code reported me a error where as when i used it as a pointer it accepted it , i searched the web and i came to know that it was called the self referential structure

#include<stdio.h>
    #include<conio.h>
    struct ko
    {
     int a;
     struct ko * op; // this was allowed by my compiler
    };

this structure worked please brief me !

I'm using a TurboC++ version 3.0 by borland international inc.

Casey Rule
  • 2,085
  • 2
  • 18
  • 28
satabios
  • 47
  • 11
  • 4
    That would cause an infinite loop! – bzeaman Dec 11 '14 at 14:06
  • @BennoZeeman More like infinite recursion – Cory Kramer Dec 11 '14 at 14:07
  • 2
    Think about it, compute the size of am, which contains itself. – Borgleader Dec 11 '14 at 14:07
  • no I'm asking that why a self referential structure is allowed where as a structure with its own variable declaration is not allowed something like this struct ko { struct ko l; }; why is this not allowed? – satabios Dec 11 '14 at 14:08
  • 1
    Try placing a 2'x 2'x 2' box inside a similar 2' x 2' x 2' box that's inside another box and so on. That's what you're trying to do in the first example. It can't work. But you can take a 2' x 2' x 2' box and place a note inside it where you write the location of another box. That's what you're doing in the 2. example with a pointer. – nos Dec 11 '14 at 14:12
  • Thank you nos i get what you're trying to say and sorry if this post was done before im just a beginner – satabios Dec 12 '14 at 17:06

2 Answers2

9

An object cannot contain an object of the same type as itself; that's a logical impossibililty, as it would need to be larger than itself.

As you say, it could contain a pointer or reference, but not an object.

I'm using a TurboC++ version 3.0 by borland international inc.

I've no idea why you'd use a twenty-year old compiler, and an ancient dialect of C++ that few now remember. But this was just as impossible then as it is now.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

Two reasons:

  1. The struct am type isn't complete until the closing } of the struct definition; when you try to declare k as an instance of struct am, the compiler still doesn't know how big struct am needs to be.

  2. A struct cannot contain an instance of itself, since that member would also have to contain an instance of itself, which would contain an instance of itself, ad inifinitum; the resulting object would be inifinitely large.

A struct can contain a pointer to an instance of the same type because a) you can create pointers to incomplete types, and b) all struct pointer types have the same size and representation.

John Bode
  • 119,563
  • 19
  • 122
  • 198