HiC : Structure within Structure? - c

this is my first time using structure within a structure.
I encounter this error when I compile my program.
error: field 'results' has incomplete type.
The error is referring to this line of code.
-->struct result_t results;
Any help please? :)
Thanks.
typedef struct {
char moduleCode[8];
char grade[3];
} result_t;
typedef struct {
char name[31];
struct result_t results;
} student_t;
Edit:
I changed my codes:
typedef struct {
char moduleCode[8];
char grade[3];
} result_t;
typedef struct {
char name[31];
result_t results;
} student_t;
and I got a new compilation error.
error : subscripted value is neither array nor pointer.
The line of code that triggered that error is as follows.
printf(" %-7s %-2s %d\n", student.results[i].module_code, student.results[i].grade, student.results[i].mc);

Result is not an array. you should either change the structure student with:
typedef struct {
char name[31];
result_t results[MAX_NUM_RESULTS];
} student_t;
Or change the printf to:
printf(" %-7s %-2s %d\n", student.results.module_code, student.results.grade, student.results.mc);
It depends on how many possible results one student may have.

since you are using typedef use this
typedef struct {
char name[31];
result_t results;<---remove struct
} student_t;

Related

How to nest structures?

typedef struct{
char name_cake[10];
char code_cake[10];
int stock_cake;
char about_cake[10];
char cake_taste[10];
}order;
typedef struct{
char name_cake[10];
char code_cake[10];
int stock_cake;
char about_cake[10];
char cake_taste[10];
}cake;
how to the contents of typedef struct become one? and I can invoke the command with
information.order.name_cake
information.cake.name_cake
for simply and not waste of words, thank
I would recommend to use the same struct for cake and order, there is no polymorphism in c
To access them in the way you like:
#include <stdio.h>
typedef struct{
char name_cake[10];
char code_cake[10];
int stock_cake;
char about_cake[10];
char cake_taste[10];
}order;
typedef struct{
char name_cake[10];
char code_cake[10];
int stock_cake;
char about_cake[10];
char cake_taste[10];
}cake;
typedef struct {
union {
order the_order;
cake the_cake;
}; // anonymous union
}information;
int main() {
order an_order = {"cakename", "code", 0, "about", "taste"};
information info = *((information*)&an_order);
printf("From order: %s\n", info.the_order.name_cake);
printf("From cake: %s\n", info.the_cake.name_cake);
return 0;
}
$ gcc -Wall ordercake.c
$ ./a.out
From order: cakename
From cake: cakename
$
In general you want to do object-oriented programming in C. Therefore, take a look at: How can I simulate OO-style polymorphism in C? (There is even a free book as pdf linked on how to do it.)
And now an example for a struct in a struct:
#include <stdio.h>
typedef struct{
char name_cake[10];
char code_cake[10];
}cake;
typedef struct{
cake the_cake; // the common part
char cake_extension[10]; // the new part, order is important
}extended_cake;
int main() {
extended_cake an_extended_cake = {{"cakename", "code"}, "extension"};
// now treat it as a cake
cake *a_normal_cake = (cake *)&an_extended_cake;
printf("From cake: %s\n", a_normal_cake->name_cake);
return 0;
}
$ gcc -Wall ordercake.c
$ ./a.out
From cake: cakename
$
Not sure what you're going to do, but you can simply nest structures like this:
struct foo {
order order;
cake cake;
}
If you want the contents of order and cake occupy the same memory space, you should use an anonymous union, as written above.
#include<stdio.h>
#include<string.h>
struct sports
{
char name[40];
int age;
/*nested structure*/
struct medicaltests
{
char isFit[1];
} med;/*nested object*/
};
int
main ()
{
struct sports sportobj;/*Declare object*/
strcpy (sportobj.name, "venkateshwar krishnan");
sportobj.age = 40;
strcpy (sportobj.med.isFit, "Y");
printf ("name of sportsman%s\n", sportobj.name);
printf ("IsFit %s", sportobj.med.isFit);
return 0;
}

Struct in C. Unknown type name,

I am new to structures in C but as far as I know, my code is "correct". I am using Codeblocks but I've also compiled it in DEV C++ and I got the same error
#include <stdio.h>
#include <stdlib.h>
struct film{
int year;
char title[30];
char director[30];
char main_char[30];
} ;
int main ()
{
film venom={ 2018, "Venom", "Ruben Fleischer", "Tom Hardy" };
printf("Year: %d\n", venom.year);
printf("Title: %s\n", venom.title);
printf("Director: %s\n", venom.director);
printf("Main Character: %s\n", venom.main_char);
system("PAUSE");
return 0;
}
I have not idea what is the error about.
This is C, not C++, so structs have their own namespace.
You need to either write struct film venom;, or use the traditional typedef:
typedef struct film film;
which is often attached to the struct definition itself;
typedef struct film{
int year;
char title[30];
char director[30];
char main_char[30];
} film;
struct film
{
...
} ;
and then
struct film f;
or
typedef struct
{
...
} film;
and then
film f;
Also I would advise you to use "s_" and "t_" to make a difference between the structure and its alias and use the "t_" alias as a type when you declare the structure.
typedef struct s_film {
int year;
char title[30];
char director[30];
char main_char[30];
} t_film;
And then use it the following way:
t_film venom = {2018, "Venom", "Ruben Fleischer", "Tom Hardy"};
You need to use:
struct film venom={ 2018, "Venom", "Ruben Fleischer", "Tom Hardy" };
Or you can typedef the struct
typedef struct {
int year;
char title[30];
char director[30];
char main_char[30];
} film;
Which allows you to use film instead of struct film

Expected primary expression before ';' token

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct PROCESS{
int priority;
int lifecycle;
int ttl;
}process1,process2,process3,process4,process5,process6;
main(){
PROCESS *waiting_queue;
waiting_queue = process1; //this is were I get the error.
waiting_queue =(PROCESS *)malloc(6*sizeof(PROCESS));
if(!waiting_queue){printf("no memory for waiting queue "); exit(0);}
getch();
}
I am trying to create a struct array with pointer. I am getting the error. Expected primary expression before ';' token
You should create your struct object from (process1 to process6).
Let me give you an example:
#include <stdio.h>
#include <string.h>
typedef struct student
{
int id;
char name[20];
float percentage;
} status;
int main()
{
status record;
record.id=1;
strcpy(record.name, "Orcun");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
return 0;
}
This is why you are getting your error in your main function. So you should create your struct object like:
process1 processOrcun;
You can also check here : https://www.codingunit.com/c-tutorial-structures-unions-typedef
You have multiple problem, but the one causing your error is that you don't define a type PROCESS, but a structure with that name.
When you use typedef to define a structure type, the type-name comes after the structure:
typedef struct
{
...
} PROCESS;
The error you have is because you define e.g. process1 as a type, so the assignment (making a pointer point to a type) doesn't make any sense.
Another, and unrelated, problem is how you define the main function. It must be defined to return an int (even if your declaration does that implicitly, it's good to do it explicitly) and to either have void for argument or an integer and an array of pointers to char. In your case it should look like
int main(void) {
...
return 0;
}

request member for 'name' in something not structure or union

i have this two structures
typedef struct pokemon_move_t {
char* name;
PokemonType type;
int power_points, max_power_points;
int strength;
} *PokemonMove;
typedef struct pokemon_t {
char* name;
PokemonType type;
int experience;
int health_points;
PokemonMove* moves;
int number_of_moves, max_number_of_moves;
} *Pokemon;
and i have a function that receives pokemon struct and i'm trying to reach the name field in the function and it shows me the error message in the title,i tried everything that suggested before and it didn't work, the function is(not complete) :
int pokemonMoveName(Pokemon pokemon){
char* name= pokemon->moves->name; //the error is in this line
return 0;
}
The element moves is:
PokemonMove * moves;
Which is:
struct pokemon_move_t ** moves;
And not:
struct pokemon_move_t * moves;
... it is a pointer to a pointer to a structure and not to a structure itself.
I think that you don't want this!
Either you have to remove the * at the typedef or the * in the struct.
If moves really is a pointer to a pointer you'll have to access it the following way (or similar):
char* name= (*(pokemon->moves))->name;
... which is equal to:
char* name= pokemon->moves[0]->name;
... or, if moves points to an array:
char* name= pokemon->moves[index]->name;
I think you mean the following declaration of the data member
typedef struct pokemon_t {
char* name;
PokemonType type;
int experience;
int health_points;
PokemonMove moves;
^^^^^^^^^^^^^^^^^
int number_of_moves, max_number_of_moves;
} *Pokemon;
In this case this statement
char* name= pokemon->moves->name;
will be valid.
The type PokemonMove is already declared like a pointer type.
typedef struct pokemon_move_t {
char* name;
PokemonType type;
int power_points, max_power_points;
int strength;
} *PokemonMove;
^^^^^^^^^^^^

error line: 43 compiler: expected identifier or '(' before ' [ ' token

I have this problem with the line 43, I dont know why
if I dont write that line, the error does not appear, I've seen the code for a while and I havent found why it appears
the error is here
/*for(i=0;i<MAX_ESTACIONES;i++){
Estaciones[i].nobici=10; //the problem is this line
}
*/
and this is the code
#include<stdio.h>
#include<time.h>
#define MAX_ESTACIONES 10
#define MAX_CARACTERES 40
#define MIN_CARACTERES 20
typedef char tipodato;
typedef struct info
{
tipodato nombre[MAX_CARACTERES];
tipodato edad[MIN_CARACTERES];
tipodato sexo[MIN_CARACTERES];
tipodato curp[MIN_CARACTERES];
tipodato domicilio[MAX_CARACTERES];
tipodato nacimiento[MAX_CARACTERES];
tipodato comentario[MAX_CARACTERES];
tipodato contrasenia[MAX_CARACTERES];
int prestamo;
struct info *sig;
}Persona;
typedef struct
{
int nobici;
clock_t inicio,fin;
} Estaciones[MAX_ESTACIONES];
typedef Persona *Listapersona;
Listapersona L;
int main()
{
Persona *posicion;
Persona *P;
P=NULL;
posicion=NULL;
int opcion,salir,i;
salir=0;
for(i=0;i<MAX_ESTACIONES;i++){
Estaciones[i].nobici=10; //the problem is this line
}
return 0;
}
The typedef makes Estaciones the name of a type, and not a variable as you intended:
typedef struct
{
int nobici;
clock_t inicio,fin;
} Estaciones[MAX_ESTACIONES];
Remove the typedef to make it a variable, or make both a type and variable:
typedef struct
{
int nobici;
clock_t inicio,fin;
} TheStructName;
TheStructName Estaciones[MAX_ESTACIONES];
Remove the typedef in typedef struct ... Estaciones[...]. You are obviously trying to define Estaciones as a type and use it as a variable. Also, instead of using typedef struct { ... } Name;, you should really use struct Name { ... }; nowadays.

Resources