Home > Blockchain >  fixing the compiler error of this snippet with structs - C
fixing the compiler error of this snippet with structs - C

Time:06-24

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;
}

CodePudding user response:

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.

CodePudding user response:

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;
  • Related