0

The "new" errors:

main.c:14:34: error: ‘p.father’ is a pointer; did you mean to use
‘->’?    14 |           p.name, p.age, p.father.name, p.mother.name);
      |                                  ^
      |                                  -> main.c:14:49: error: ‘p.mother’ is a pointer; did you mean to use ‘->’?    14 |          
p.name, p.age, p.father.name, p.mother.name);
      |                                                 ^
      |                                                 -> main.c: In function ‘main’: main.c:28:19: error: incompatible types when
assigning to type ‘struct Person *’ from type ‘struct Person’    28 | 
bart.mother = marge;
      |                   ^~~~~ main.c:29:19: error: incompatible types when assigning to type ‘struct Person *’ from type ‘struct
Person’    29 |     bart.father = homer;
      |                   ^~~~~

I need to fix this snippet so that it works. On the first run the compiler shows me this:

main.c:7:19: error: field ‘mother’ has incomplete type
    7 |     struct Person mother;
      |                   ^~~~~~  main.c:8:19: error: field ‘father’ has incomplete type
    8 |     struct Person father;
      |

            ^~~~~~
#include <stdio.h>


struct Person {
    int age;
    char* name;
    struct Person mother; //i guess it's somthing wrong here
    struct Person father; //i guess it's somthing wrong here
};


void showPerson(struct Person p) {
    printf("Name: %s\nAge: %d\nFather: %s, Mother: %s\n",
          p.name, p.age, p.father.name, p.mother.name); }


int main() {
    struct Person homer; //and here also wrong
    struct Person marge; //and here also wrong
    struct Person bart;  //and here also wrong
    homer.age = 36;
    homer.name = "Homer";
    marge.age = 34;
    marge.name = "Marge";
    bart.age = 10;
    bart.name = "Bart";
    bart.mother = marge;
    bart.father = homer;
    showPerson(bart);
    return 0;
}
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
Catty_cat
  • 7
  • 2
  • 1
    Please don't copy the code with leading `>` or line numbers or similar. Just plain text is OK. – Gerhardh Jun 24 '22 at 06:12
  • 2
    You cannot include a struct within itself. You probable want to have pointers: `struct Person *mother; struct Person *father;` – Gerhardh Jun 24 '22 at 06:13
  • 2
    Does this answer your question? [self referential struct definition?](https://stackoverflow.com/questions/588623/self-referential-struct-definition) – CloudBalancing Jun 24 '22 at 06:17
  • Or this - https://stackoverflow.com/questions/506366/c-pointer-to-struct-in-the-struct-definition – CloudBalancing Jun 24 '22 at 06:24
  • 1
    The top voted answer of that first duplicate link also contains an example how to use such a struct with pointers. You need to revisit pointer basics in your text book or tutorial etc. – Gerhardh Jun 24 '22 at 06:42

2 Answers2

3

As already explained, you cannot have a structure containing itself. You need to use pointers here.

You probably want this:

#include <stdio.h>

struct Person {
  int age;
  char* name;
  struct Person *mother;  // use pointer here
  struct Person *father;  // use pointer here
};


void showPerson(struct Person p) {
  printf("Name: %s\nAge: %d\nFather: %s, Mother: %s\n",
    p.name, p.age, p.father->name, p.mother->name);
  //                       ^                ^  pointer
}


int main() {
  struct Person homer;
  struct Person marge;
  struct Person bart;
  homer.age = 36;
  homer.name = "Homer";
  marge.age = 34;
  marge.name = "Marge";
  bart.age = 10;
  bart.name = "Bart";
  bart.mother = &marge;   // use pointer here
  bart.father = &homer;   // use pointer here
  showPerson(bart);
  return 0;
}

Explanation in the comments.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • @Catty_cat Be aware that if you want to return such a struct and use it after leaving that function where you initialize it, the memory where your pointers point to must still be valid. Lookup lifetime of data objects for details. – Gerhardh Jun 24 '22 at 07:45
1

In C a struct cannot refer to itself directly. This some times referred to as a recursive struct. Combining the two famous answers in the comment on your original question these are the two options:

The only way to create a recursive structure is to use pointers. And adding forward declaration

typedef struct Person Person;

struct Person {
    int age;
    char* name;
    struct Person *mother; 
    struct Person *father;
};

Or

typedef struct Person {
    int age;
    char* name;
    struct Person *mother; 
    struct Person *father;
} Person;
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
CloudBalancing
  • 1,461
  • 2
  • 11
  • 22
  • You could improve this answer by adding some information about other required chances to make it work. – Gerhardh Jun 24 '22 at 06:16
  • Thank u very much. But like the comment above says - just the change of this don't fix the code. I'm sorry I really have no clue, i already tried a lot – Catty_cat Jun 24 '22 at 06:22
  • See my update about forward declaration – CloudBalancing Jun 24 '22 at 06:24
  • @Catty_cat after changing to pointers you must also assign pointers to these members instead of the variables itself. Use `malloc` to get memory for the structs (and free afterwards...) That should be covered on any C text book about pointers and dynamic memory allocation. – Gerhardh Jun 24 '22 at 06:37
  • thank u, but I added a few "new" errors to my question which shows after the changes... :( – Catty_cat Jun 24 '22 at 06:38
  • 2
    @Catty_cat Your new errors do not match the code any more. And generally if you have problems using pointers that is a completely different question. Don't change this one making it a moving target. – Gerhardh Jun 24 '22 at 06:41
  • i just changed the things u recommended and now these errors shows. Sure it is the same code but with the few changes from you? :D – Catty_cat Jun 24 '22 at 06:46
  • @Catty_cat you also got a link to duplicate question. Read it. Including the answers showing how the changed struct is used. StackOverflow cannot replace learning the very basic concepts of a programming language. – Gerhardh Jun 24 '22 at 06:51