#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;
}
Related
I'm trying to declare structures in my main() that I built in a separate file, but am getting the error:
error: storage size of 'phone' isn't known
Here is the code for main.c:
#include "header.h"
int main(int argc, char **argv)
{
struct firstAndLast name;
struct contact phone;
struct address adr;
struct allInfo individual;
print_person(individual);
return 0;
}
This is the function.c file that I wrote the structures in:
#include "header.h"
struct firstAndLast
{
char firstName[20];
char lastName[20];
};
struct contact
{
int pNumber;
};
struct address
{
struct firstAndLast person;
struct contact phoneNumber;
char streetAddress[100];
char city[50];
char province[50];
char postalCode[10];
};
struct allInfo
{
struct address addr;
char occupation[50];
double salary;
};
void print_person(struct allInfo indiv)
{
printf("%s \n",indiv.addr.person.firstName);
}
And this is the header.h file:
#ifndef nS
#define nS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct firstAndLast;
struct contact;
struct address;
struct allInfo;
void print_person(struct allInfo indiv);
#endif
I'm not sure why I am getting this error. I put all of the functions in the header file and used #include "header.h" for both my main.c and functions.c files, so it should recognize the structures exist. Is there a problem with the way that I declared the structures in main() or how I listed them in my header? I don't see any typos in my code, so I'm really lost and don't know what I'm doing wrong.
First, you need to move full struct definitions into the header file.
#pragma once
#ifndef nS
#define nS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct firstAndLast
{
char firstName[20];
char lastName[20];
};
struct contact
{
int pNumber;
};
struct address
{
struct firstAndLast person;
struct contact phoneNumber;
char streetAddress[100];
char city[50];
char province[50];
char postalCode[10];
};
struct allInfo
{
struct address addr;
char occupation[50];
double salary;
};
void print_person(struct allInfo indiv);
#endif
Moreover, please initialize each variable before using them.
For example:
struct firstAndLast name;
strcpy(name.firstName, "firstNameA");
strcpy(name.lastName, "lastNameB");
struct contact phone;
phone.pNumber = 01234567;
struct address adr;
adr.person = name;
adr.phoneNumber = phone;
strcpy(adr.streetAddress, "ABC street");
strcpy(adr.city, "DEF city");
strcpy(adr.province, "GH");
strcpy(adr.postalCode, "12345");
struct allInfo individual;
individual.addr = adr;
strcpy(individual.occupation, "Occupation");
individual.salary = 1234.56;
print_person(individual);
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.
I have the following simple code. I am trying to understand how I can use structs inside unions and how I can retrieve contents of struct variables in connection with unions.
Here is a small sample code I have written. I want to retrieve the "Maker" struct variables from this code. How should I correctly do it? My code here results in segmentation fault.
Here is the updated code :
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char *name;
int *wheels;
}CarVendor;
typedef struct{
char *name;
int *wheels;
int seats;
}BusVendor;
typedef union{
CarVendor *carvendor;
BusVendor *busvendor;
}Maker;
typedef struct{
Maker *carType;
}Car;
typedef struct{
Maker *busType;
}Bus;
typedef union{
Car *car;
Bus *bus;
}Vehicle;
void fillDetails(Vehicle *vehicle, int type){
if(type == 0){
vehicle->car->carType->carvendor->name = "car";
int wheel = 4;
vehicle->car->carType->carvendor->wheels = &wheel;
}
if(type ==1){
vehicle->bus->busType->busvendor->name = "bus";
int wheel = 6;
vehicle->bus->busType->busvendor->wheels = &wheel;
vehicle->bus->busType->busvendor->seats = 60;
}
}
int main(int argc, char *argv[])
{
Vehicle myvehicle;
fillDetails(&myvehicle, 0); //get car details filled & retrieve the details as "Maker" struct
Maker *maker;
maker = (Maker *) malloc(sizeof(Maker));
maker = myvehicle.car->carType;
printf("Name of car =%s", maker->carvendor->name);
return 0;
}
Are you sure you want everything to be on the heap?
If you really want to, you need to malloc memory for ALL the structures that you've got pointers to (or at least the ones you want to access, e.g. from fillDetails), and clean up afterwards. Your union, e.g. Maker, essentially just says that this pointer can be one of two types, either a CarVendor, or a BusVendor.
If you want to do this kind of stuff it sometimes makes sense to have a "type" distinguisher in the structure, such that the code looking at it later knows which type the pointer has. Here's an example:
typedef struct a_t_ { /*...*/ } a_t;
typedef struct b_t_ { /*...*/ } b_t;
typedef enum mytype_t_ {a, b} mytype_t;
typedef struct foo_t_ {
mytype_t type_of_the_below;
union {
a_t *a;
a_t *b;
};
} foo_t;
Again, notice that you need to malloc the memory for the Maker before you access it from fillDetails, not after.
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char *name;
int *wheels;
}CarVendor;
typedef struct{
char *name;
int *wheels;
int seats;
}BusVendor;
typedef union{
CarVendor *carvendor;
BusVendor *busvendor;
}Maker;
typedef struct{
Maker *carType;
}Car;
typedef struct{
Maker *busType;
}Bus;
typedef union{
Car *car;
Bus *bus;
}Vehicle;
void fillDetails(Vehicle *vehicle, int type){
if(type == 0){
vehicle->car->carType->carvendor->name = "car";
int wheel = 4;
vehicle->car->carType->carvendor->wheels = &wheel;
}
if(type ==1){
vehicle->bus->busType->busvendor->name = "bus";
int wheel = 6;
vehicle->bus->busType->busvendor->wheels = &wheel;
vehicle->bus->busType->busvendor->seats = 60;
}
}
int main(int argc, char *argv[])
{
Vehicle myvehicle;
myvehicle.car = (Car*) malloc(sizeof(Car));
myvehicle.car->carType = (Maker*)malloc(sizeof(Maker));
myvehicle.car->carType->carvendor = (CarVendor*) malloc(sizeof(CarVendor));
fillDetails(&myvehicle, 0); //get car details filled & retrieve the details as "Maker" struct
Maker *maker = NULL;
maker = myvehicle.car->carType;
printf("Name of car =%s\n", maker->carvendor->name);
free(myvehicle.car->carType->carvendor);
free(myvehicle.car->carType);
free(myvehicle.car);
return 0;
}
Here's how you can more safely do it on the stack...
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char *name;
int wheels;
}CarVendor;
typedef struct{
char *name;
int wheels;
int seats;
}BusVendor;
typedef union{
CarVendor carvendor;
BusVendor busvendor;
}Maker;
typedef struct{
Maker carType;
}Car;
typedef struct{
Maker busType;
}Bus;
typedef union{
Car car;
Bus bus;
}Vehicle;
void fillDetails(Vehicle *vehicle, int type){
if(type == 0){
vehicle->car.carType.carvendor.name = "car";
int wheel = 4;
vehicle->car.carType.carvendor.wheels = wheel;
}
if(type ==1){
vehicle->bus.busType.busvendor.name = "bus";
int wheel = 6;
vehicle->bus.busType.busvendor.wheels = wheel;
vehicle->bus.busType.busvendor.seats = 60;
}
}
int main(int argc, char *argv[])
{
Vehicle myvehicle;
fillDetails(&myvehicle, 0); //get car details filled & retrieve the details as "Maker" struct
Maker *maker;
maker = &myvehicle.car.carType;
printf("Name of car =%s\n", maker->carvendor.name);
return 0;
}
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;
}
I'm trying to create a structure, that has a function pointer. That function pointer points to a function, that takes a pointer of said structure. This is a real chicken-or-the-egg problem because the prototype needs to know about the structure and the structure needs to know about the prototype. Is there a way to predefine the struct? I'm new to C so if anyone has any insight I would greatly appreciate it.
Thanks,
-devnull
#include <stdio.h>
/* predefine struct person? */
void printInfo(struct person *);
struct person{
char *name;
int age;
const void *pf = printInfo;
};
int main(){
struct person master = {"Master", 23};
return 0;
}
void printInfo(struct person *p){
printf("Name:\t%s\n", p->name);
}
struct person;
typedef void (*fp)(struct person*);
struct person {
char * name;
fp fptr;
};
void afunc( struct person * p ) {
// stuff
}
int main() {
struct person p = { "fred", afunc };
}
You can add the struct person; before the function, but you cannot assign the function in struct person as far as I know,
#include <stdio.h>
struct person;
typedef void (FUNCTYPE)(struct person *);
void printInfo(struct person *);
struct person{
char *name;
int age;
FUNCTYPE *pf;
};
int main(){
struct person master = {"Master", 23, printInfo};
(master.pf)(&master);
return 0;
}
void printInfo(struct person *p){
printf("Name:\t%s\n", p->name);
}
The example above prints Name: Master
The only thing I would add is that all struct pointers have the same width and alignment (6.2.5/27 (C99 Standard)), so you don't actually require the forward definition of the struct.