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;
}
5
  • 1
    Please don't copy the code with leading > or line numbers or similar. Just plain text is OK. Commented Jun 24, 2022 at 6:12
  • 2
    You cannot include a struct within itself. You probable want to have pointers: struct Person *mother; struct Person *father; Commented Jun 24, 2022 at 6:13
  • 2
    Does this answer your question? self referential struct definition? Commented Jun 24, 2022 at 6:17
  • Or this - stackoverflow.com/questions/506366/… Commented Jun 24, 2022 at 6: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. Commented Jun 24, 2022 at 6:42

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@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.
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;

8 Comments

You could improve this answer by adding some information about other required chances to make it work.
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
See my update about forward declaration
@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.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.