Reputation: 57
Today I want to try something out what I learned in struct but unfornuate it didnt compiled ...
this here is my code
struct Addresse
{
char street[20];
};
struct Person
{
char name[20];
int age;
char job[20];
struct Addresse mainstreet;
};
void callbyReference(struct Person* p)
{
printf("%s,%d,%s,%s\n",p->name,p->age,p->job,p->mainstreet.street );
}
int main(int argc, char const *argv[])
{
struct Person *person;
person = (struct Person*) malloc(sizeof(struct Person));
strcpy(person.name, "Max");
person.age = 14;
strcpy(person.job,"firefighter");
strcpy(person.mainstreet.street,"Fressnitz");
callbyReference(&person);
free(person)
return 0;
}
When i compiled it there is this error warning: request for member job in something not a structure or union.
Upvotes: 0
Views: 137
Reputation: 21
To access the elements of the structure person, you should use ->
, not the .
operator.
Just as you did on line 21; p->mainstreet.street
, this is how you should use your arrow selection operator(->
) and the dot(.
). In this case, the dot operator was used to access the members of another structure, namely Address
while the arrow selection operator was used to access the members of its outer structure.
Here's the code that works, as a modification to your code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Addresse
{
char street[20];
};
struct Person
{
char name[20];
int age;
char job[20];
struct Addresse mainstreet;
};
void callbyReference(struct Person* p)
{
printf("%s,%d,%s,%s\n",p->name,p->age,p->job,p->mainstreet.street );
}
int main(int argc, char const *argv[])
{
struct Person *person;
person = (struct Person*) malloc(sizeof(struct Person));
strcpy(person->name, "Max");
person->age = 14;
strcpy(person->job,"firefighter");
strcpy(person->mainstreet.street,"Fressnitz");
callbyReference(person);
free(person);
return 0;
}
Upvotes: 0
Reputation: 596256
There are two mistakes in your code.
After you allocate the struct Person
instance with malloc()
and assign it to your person
variable, you are accessing its members using the .
operator, but since person
is a pointer you need to use the ->
operator instead:
struct Person *person;
...
strcpy(person->name, "Max");
person->age = 14;
strcpy(person->job,"firefighter");
strcpy(person->mainstreet.street,"Fressnitz");
Once you fix that, you will get a new error, because callbyReference()
expects a struct Person *
pointer, ie a single pointer directly to an actual Person
instance, but you are passing it a struct Person **
pointer instead, ie a pointer to a pointer to a Person
instance:
struct Person *person;
...
callbyReference(&person);
So, get rid of the &
address operator when passing your person
variable to callbyReference()
, as it is already a suitable pointer as-is:
callbyReference(person);
Your code would be using the .
and &
operators correctly if you had allocated person
on the stack instead of via malloc()
, eg:
int main(int argc, char const *argv[])
{
struct Person person;
strcpy(person.name, "Max");
person.age = 14;
strcpy(person.job,"firefighter");
strcpy(person.mainstreet.street,"Fressnitz");
callbyReference(&person);
return 0;
}
Upvotes: 3