Structure within structure and function in C - c

I've been doing the task of reading data and creating particular structures.
In one structure (which contains in itself another structure) eclipse shows "field 'birth' has incomplete type".
I've searched through web, but it looks like there is some specific mistake.
(Here is shortened version of the code)
typedef struct{
int birthday_day;
int birthday_month;
int birthday_year;
} birthday;
typedef struct{
int id;
char name[20];
struct birthday birth;
}user;
user usser[100];
int i;
for (i=0;i<100;i++){
fscanf(input, "%s %i %i %i %i", usser[i].id,
usser[i].name, usser[i].birth.birthday_day, usser[i].birth.birhday_month,
usser[i].birth.birthday_year
};

typedef struct _birthday{
int birthday_day;
int birthday_month;
int birthday_year;
} birthday;
typedef struct{
int id;
char name[20];
struct _birthday birth;
}user;
or
typedef struct{
int id;
char name[20];
birthday birth;
}user;
in your example "birthday" is a new type which doesn't need the keyword "struct". That's why you get the error. You can use this type or give a name to the struct and use it with the keyword struct.

Related

making an array with a specific size that includes elements in data structure datatype

I want to make an array with 100 elements that include structure data type in C
#include <stdio.h>
struct student{
char name[20];
float grade[15];
struct date{
int day;
int month;
int year;
};
};
int main(){
struct std[100];
}
With a structure type like this:
struct student{
char name[20];
float grade[15];
struct date_tag{
int day;
int month;
int year;
} date;
};
You can do things like this
int main(){
struct student std[100];
std[99].grade[14]=1.5;
std[99].grade[14].date.day=28;
}
The identifier date_tag in that code is called a struct tag.
The corresponding type is struct date_tag and it is used to define a member of of that type within the the type struct student. The member is called date.
Within main() an array of struct students is defined and then accessed for demonstration purposes.

typedef struct format in another typedef struct

How can I declare a typedef struct in other typedef struct?
typedef struct {
char* type;
char* destination;
float price;
typedef struct {
int date;
int month;
int year;
}date;
}Offer;
I tried something like this but I don't know if its correctly. I want to declare in typedef struct another typedef struct for date format.
Try this:
typedef struct {
char* type;
char* destination;
float price;
struct {
int day;
int month;
int year;
} date;
} Offer;
This defines date as being of an anonymous struct. If you need to have this struct available elsewhere use Shark's approach.
Instead of what you wrote, let's try something like this:
typedef struct {
int date;
int month;
int year;
} OfferDate;
typedef struct {
char* type;
char* destination;
float price;
OfferDate date;
} Offer;
Thats incorrect syntax. If you want to define a struct using another struct, you can do it like this:
typedef struct {
int a1;
int a2;
} ExampleStruct1;
typedef struct {
int b1;
ExampleStruct1 b2;
} ExampleStruct2;

How to use a union along with two structs + more

I'm having trouble with my assignment and was hoping to get some help.
I'm suppose to have two structs, volunteer and employee, and a union 'person' that takes firstname, lastname, telenumber + either volunteer or employee as a struct.
I have had experience using unions + structs before, but I'm suppose to have additional information in the union. I was wondering how I can set it up properly, this is what I have so far.
typedef struct volunteer{
int hours;
int tasksCompleted;
}student;
typedef struct employee{
float salary;
int serviceYears;
int level;
}employee;
typedef union person{
char firstName[20];
char familyName[20];
char telephoneNum[10];
//employee e;
//volunteer;
}person;
Any help would be great. This is the task instruction I'm stuck on.
Create a person record that consists of the common records: first name, family name and telephone and a union between the volunteer and employee record. Make sure that you add a field to discriminate between the two records types employee or volunteer.
I think you are overthinking this: you need a structure to represent person. The key part is
"and a union between the volunteer and employee record."
typedef enum { employee_person, volunteer_person } person_type;
typedef struct person{
char firstName[20];
char familyName[20];
char telephoneNum[10];
person_type type;
union {
struct employee employee;
struct volunteer volunteer;
};
}person;
This should do what you ask:
typedef struct volunteer {
int hours;
int tasksCompleted;
} student;
typedef struct employee {
float salary;
int serviceYears;
int level;
} employee;
struct person {
char firstName[20];
char familyName[20];
char telephoneNum[10];
bool is_employee;
union {
employee e;
student s;
} info;
} person;

Can't print structure variable

#include <stdio.h>
#include <stdlib.h>
typedef struct {
char name[20];
int age;
} employee;
int main(int argc, char** argv)
{
struct employee em1 = {"Jack", 19};
printf("%s", em1.name);
return 0;
}
This doesn't seem to work because, as the compiler says, the variable has incomplete type of 'struct employee'. What's wrong?
Remove struct from
struct employee em1 = {"Jack", 19};
You used
typedef struct
{
char name[20];
int age;
}
with the purpose of not requiring to type struct anymore.
The problem is that you made the struct a typedef, but are still qualifying it with struct.
This will work:
employee em1 = {"Jack", 19};
Or remove the typedef.
To use struct employee em1 = ... you need to declare the struct with a tag.
struct employee /* this is the struct tag */
{
char name[20];
int age;
} em1, em2; /* declare instances */
struct employee em3;
typedef creates a type alias which you use without the struct keyword.
typedef struct employee employee;
employee em4;
As you have already typedef your structure, you are not required to add struct keyword again.
typedef struct Employee{
char name[20];
int age;
} employee;
int main(int argc, char** argv)
{
employee em1 = {"Jack", 19};
printf("%s", em1.name);
return 0;
}

use a struct into an other struct c

I have a problem using a struct in the C language.
It is very strange !!!
I cant use course struct in student struct.
I have defined it before but ...
why?
struct course
{
int no;
char name[30];
int credits;
float score;
};
struct student
{
int no;
char name[50];
course c[3];
};
My language is c not c++
One of the differences between C++ and C is that you can omit type keywords such as class and struct when using C++ types.
The problem is the line course c[3];. In order to make it work, you have two choices--you can use a typedef on your struct course:
typedef struct _course // added an _ here; or we could omit _course entirely.
{
int no;
char name[30];
int credits;
float score;
} course;
or you can add the keyword struct in front of the broken line, i.e. structcourse c[3];.
You need to prefix the struct name with the struct keyword:
struct course
{
int no;
char name[30];
int credits;
float score;
};
struct student
{
int no;
char name[50];
struct course c[3];
};
struct course c[3];
should work...
struct student {
/* ... */
struct course c[3];
}
or
typedef struct _course {
/* ... */
} course;
struct student {
/* ... */
course c[3];
}
You should actually be able to define an anonymous struct and then typedef it, so:
typedef struct {
/* stuff */
} course;
and then as the others have said,
struct student {
course c[3];
}
typedefs are helpful, because they allow you to shorten declarations, so you are not always having to type the word struct.
Here is an example involving typedef-ing your structs. It also includes a course struct in the student struct.
#include <stdio.h>
#include <string.h>
typedef struct course_s
{
int no;
char name[30];
int credits;
float score;
} course;
typedef struct student_s
{
int no;
char name[50];
course c[3];
} student;
bool isNonZero(const int x);
int main(int argc, char *argv[])
{
int rc = 0;
student my_student;
my_student.c[0].no = 1;
return rc;
}

Resources