Struct in C. Unknown type name, - c

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

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;
}

Is this a correct/safe way to define and use "interfaces" in pure C

for the past few months I've been trying to code in pure C and avoid C++ as much as I can (for personal reasons, C++ is a nice language in deed), but once in a while there comes situations where I miss some concepts that I used to be using a lot back when I coded in C++, which there is no obvious equivalent in C for them, one of those concepts being "Interface". so after a few hours of research I came up with this solution but I'm afraid that my code might go wrong sometime in the future. Below is the sample code of what I want to do:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef int (*on_affected_cb) (const char *msg);
typedef struct affectee_t affectee_t;
typedef struct type_a_t type_a_t;
typedef struct type_b_t type_b_t;
typedef struct type_c_t type_c_t;
typedef struct owner_t owner_t;
//******************************************************************************
struct affectee_t{ //interface
on_affected_cb on_affected;
};
void affectee_notify(affectee_t* self, const char* msg){
self->on_affected(msg);
}
//******************************************************************************
struct type_a_t{ //implementor a
affectee_t affectee;
int num;
};
static int type_a_on_affected (const char *msg){
printf("this is type a reading msg: %s\n", msg);
return 1000;
}
void type_a_init(type_a_t* self){
self->num = 0;
self->affectee.on_affected = type_a_on_affected;
}
//******************************************************************************
struct type_b_t{ //implementor b
affectee_t affectee;
char name[128];
};
static int type_b_on_affected (const char *msg){
printf("this is type b reading msg: %s\n", msg);
return 2000;
}
void type_b_init(type_b_t* self){
memset(self->name, 0, sizeof(self->name));
self->affectee.on_affected = type_b_on_affected;
}
//******************************************************************************
struct owner_t{ // ordinary struct/class
type_a_t ta;
type_b_t tb;
};
void owner_init(owner_t* self){
type_a_init(&self->ta);
type_b_init(&self->tb);
}
void owner_notify(owner_t* self){
const char msg[] = "what the f...!";
affectee_notify((affectee_t*)&self->ta, msg); //<- pointer casting!!!
affectee_notify((affectee_t*)&self->tb, msg); //<- same here
}
//******************************************************************************
int main(int argc, char **argv){
owner_t owner;
owner_init(&owner);
owner_notify(&owner);
}

HiC : Structure within Structure?

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;

Structures - Two or more data types in declaration specifiers (I have semi-colons)

Not really sure what's causing this, all I've found on google is that forgetting a semi-colon at the end of my struct causes this but I have one there.
Here's the chunk of code...
#include <stdio.h>
#include <string.h>
#define NAME_LENGTH 20
#define BOOK_NAME_LEN 50
#define AUTHOR_NAME_LEN 30
enum bookStatus {CHECKED_IN, CHECKED_OUT, UNDER_REPAIR, LOST}
enum patronStatus {ACTIVE, INACTIVE}
struct Book{
char title[BOOK_NAME_LEN];
char author[AUTHOR_NAME_LEN];
enum bookStatus status;
};
struct Name{
char first[NAME_LENGTH];
char last[NAME_LENGTH];
};
struct Patron{
int numBooksOut;
struct Name name;
struct Book books[50];
enum patronStatus status;
};
struct Collection{
struct Book book;
char title[BOOK_NAME_LEN];
char author[AUTHOR_NAME_LEN];
int id, year;
enum bookStatus status;
};
struct Library{
int totalPatrons, totalBooks;
struct Patron patrons[50];
struct Collection collection[50];
};
The enums need a semicolon, too.

What am I doing wrong? (C programming, pointers, structs, functions)

I am unsure if my use of malloc is correct, but what bother's me is the inability to pass the struct into the put_age() function pointer. It looks right to me but apparently it isn't.
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int age;
// NPC methods
int (*put_age)(NPC *character, int age);
} NPC;
////////////////////////////////////
int set_age(NPC *character, int age);
int main(){
NPC *zelda = malloc(sizeof(NPC));
zelda->put_age = set_age;
zelda->put_age(zelda, 25);
printf("Zelda's age is %d\n", zelda->age);
return 0;
}
int set_age(NPC *character, int age){
character->age = age;
return 0;
}
COMPILER OUTPUT:
$ gcc ~/test.c
/test.c:7:21: error: expected ‘)’ before ‘*’ token
/test.c:8:1: warning: no semicolon at end of struct or union
/test.c: In function ‘main’:
/test.c:16:8: error: ‘NPC’ has no member named ‘put_age’
/test.c:17:8: error: ‘NPC’ has no member named ‘put_age’
Your problem is that NPC isn't the name of a type until the declaration of the struct typedef is complete. You can change this by giving the struct a name, e.g.
typedef struct tagNPC {
int age;
// NPC methods
int (*put_age)(struct tagNPC *character, int age);
} NPC;
or
typedef struct tagNPC NPC;
struct tagNPC {
int age;
// NPC methods
int (*put_age)(NPC *character, int age);
};
I don't think you can use typedef "NPC" inside the struct def. That's because until the compiler has not seen the closing "}" , it has no idea what NPC is.
Please try changing:
typedef struct{
int age;
// NPC methods
int (*put_age)(NPC *character, int age);
} NPC;
to:
typedf struct node_npc NPC;
struct node_npc
{
int age;
int (*put_age)(NPC *character, int age);
};
Try changing this:
int set_age(NPC *character, int age){
zelda->age = age;
return 0;
}
To:
int set_age(NPC *character, int age){
character->age = age;
return 0;
}
In set_age(), your variable's name is character, not zelda, so the code should be:
int set_age(NPC *character, int age){
character->age = age;
return 0;
}
I had that problem, when I had a constant defined someware in my code that was named like a member of a struct. Ex.
#define N 10
struct my_struct{
int N;
};

Resources