Reputation: 64
I've create a structure Person and had these variables in it, then I added a new field named father and its type is pointer to a person. I have to initialize the data of FJames as following: fname = Whatever, lname = Bond, age = 80, job = Farmer, father = NULL
Then initialize the data of James as following: fname = James, lname = Bond, age = 40, job = Actor, father = FJames
Then display all the data. I'm getting an error " initializing struct Person * with an expression of incompatible type "Person" What to do? :/ I don't even think I'm doing it right, please help!
/#include <stdio.h>
typedef struct {
int age;
char *fname;
char *lname;
char *job;
struct Person *father;
}Person;
int main(int argc, const char * argv[])
{
Person James;
Person FJames = {80,"Whatever","Bond","Painting",NULL};
James.age = 40;
James.fname = "James";
James.lname = "Bond";
James.job = "Engineering";
James.father = FJames;
}
Upvotes: 1
Views: 98
Reputation: 4418
That last line should be
James.father = &FJames;
The father
field is a pointer, but FJames is a Person
. You can use &
to get the address of FJames
.
Edit
The struct definition should also be changed in addition to that, in one of the ways aragaer suggested, e.g.:
typedef struct s_Person {
// ...
struct s_Person *father;
} Person;
Upvotes: 0
Reputation: 96984
Here's a working program:
#include <stdio.h>
#include <stdlib.h>
typedef struct Person {
int age;
char *fname;
char *lname;
char *job;
struct Person *father;
} Person;
int main(int argc, const char * argv[])
{
Person James;
Person FJames = {80,"Whatever","Bond","Painting",NULL};
James.age = 40;
James.fname = "James";
James.lname = "Bond";
James.job = "Engineering";
James.father = &FJames;
fprintf(stdout, "%d\t%s\t%s\t%s\tpops:\t%s\t%s\n", James.age, James.fname, James.lname, James.job, (James.father)->fname, (James.father)->lname);
return EXIT_SUCCESS;
}
Corrections:
typedef
so that it declares Person
.James.father
it should dereference FJames
so that you are setting it to the value of the pointer to FJames
.main()
function should return an int
, so return EXIT_SUCCESS
(defined in stdlib.h
) to note that you exited properly.Advice:
James.father
, use precedence and arrow notation to dereference its values.gcc
, compile with -Wall
option to enable all compilation warnings. This will help note warnings that point where corrections are needed.Upvotes: 0
Reputation: 8444
James.father = &FJames should be what you need.
There are other minor problems. These days initialising a char* from a string constant is frowned upon, because char* implies that the memory being pointed at can be altered.
Upvotes: 0
Reputation: 17858
You don't declare struct Person
actually.
You're declaring anonymous structure and typedef it to person. You then should use it as just Person
, not struct Person
.
struct Person {
struct Person *father; // this will work
}
or if you want typedef
typedef struct s_Person {
struct s_Person *father
} Person;
Upvotes: 1
Reputation: 5612
typedef struct {
int age;
char *fname;
char *lname;
char *job;
struct Person *father; // << This is a pointer to a Person
} Person;
James.father = FJames;
FJames
is not a Person*
. He's a Person
. You need to malloc
him in order to get a Person*
. Or take his address with &
This code has some other issues, but that's the one that's giving you the error in question.
Upvotes: 0