different formats to represent the structures - c

I have watched videos by different people on youtube and now i am little bit confused
first two[(1) (2)] are of same type without typedef and then two[(3) (4)] are of same type with typedef but the way of writing them is different
Please mention if any of the below represented structure is wrong and why with number. I am assigning number to all of the these formats
(1)
struct tag{
int number;
char *name;
float num;
};
main()
{
struct tag name;
name firstmember;
------code-------
return 0;
}
Next type i have seen is same as the above but :
(2)
struct tag{
int number;
char *name;
float num;
}name;
main()
{
name firstmember;
------code-------
return 0;
}
Next is using structure with typedef:
(3)
typedef struct tag{
int number;
char *name;
float num;
}name;
main()
{
name firstmember;
------code-------
return 0;
}
same version of typedef with other one:
(4)
struct tag{
int number;
char *name;
float num;
};
main()
{
typedef struct tag name;
name firstmember;
------code-------
return 0;
}
Please mention if any of the the above represented examples have any kind of error
Lastly this doubt is completely different from the above examples
if we don't put a structure tag just after the word struct and do like this
(5)
struct {
int number;
char *name;
float num;
}tag;
main(){}
does this word 'tag' remains structure tag or if its after '}' so its a structure name now ? What is it? If it becomes the name why do we use the structure tags anyways?
Please respond to this question after reading all my doubts carefully in well written format

Have a look at these close threads with popular answers:
Difference between 'struct' and 'typedef struct' in C++?
typedef struct vs struct definitions
Why should we typedef a struct so often in C?
struct and typedef in C versus C++

Related

Passing a struct to function in diffrent file in c

I have two seperat files
in main function file 1:
struct entity{
unsigned int pos_x;
unsigned int pos_y;
unsigned int health;
} player;
struct inventory{
char helmet[length_item_names];
char body[length_item_names];
char legs[length_item_names];
char items[inventory_space];
} items;
render_screen(rendered_size_x, inventory_size_x, screen_size_y, map, player, items);
file 2:
void render_screen(int render_size_x, int inventory_size_x, int screen_size_y, char **field, struct entity player, struct inventory items){
...
}
when declaring render_screen i either get a error telling me that the struct entity and struct inventory can't be declared as incomplete data types or when i do
extern struct Player{
...
}
it tells me that the data type i give as a parameter is wrong
how can i give the struct as a prameter
The comment of Barmar workerd.
I had to put the declaration of the struct into a header file that i had to include in both c files.
Thanks for the quick help

Visual C 2012: I am getting weird errors. Code Segment and Error provided

I am writing a game in C. But I keep getting a number of errors. One prominent one is this. "Illegal Identifier: X" where X is the type of a variable inside a structure. An instance, Illegal Identifier: MapItem, is caused by the following code
typedef struct MapItem{
int item;
int count;
};
typedef struct MapTile{
MapItem item;
int x;
int y;
int tile;
Direction dir;
int drop;
};
The error is attached to the first line inside the MapTile struct. I would like to know why this error is occurring, and how to fix it.
The code segment was taken, in exact order, from map.h. Direction is an enum declared earlier in the same header.
Thank you all for answering. However, I did receive the answer I needed 4 hours ago.
Your typedef are incorrect. The syntax is:
typedef type typealias ;
So:
typedef struct
{
int item;
int count;
} MapItem;
typedef struct
{
MapItem item;
int x;
int y;
int tile;
Direction dir;
int drop;
} MapTile;
Note that types being aliased here are anonymous structs, the struct-tag is only required for self-referencing structs.
typedef is used to create an alias to another type (existing or defined in the same typedef statement).
Its general format is:
typedef existing_or_new_type alias;
The aliased type in your typedef is the new struct MapItem defined in the typedef statement but the alias is missing. This is the cause of the error.
You can use:
typedef struct MapItem {
int item;
int count;
} MapItem;
This statement declares the new type struct MapItem (the struct keyword is part of the type name) and the new type MapItem that is an alias of struct MapItem. This means that everywhere you can or have to use struct MapItem you can use MapItem instead.
If this seems confusing, you can use different names for the struct type and its alias. Or you can omit the name from the struct definition at all:
typedef struct {
int item;
int count;
} MapItem;
This way, MapItem is the name of an anonymous struct type and it is the only way to declare variables of this type.

"warning: useless storage class specifier in empty declaration" in struct

typedef struct item {
char *text;
int count;
struct item *next;
};
So I have this struct with nodes defined as above, but Im getting the error below and Im not able to figure out whats wrong.
warning: useless storage class specifier in empty declaration
};
I'm not sure, but try like that :
typedef struct item {
char *text;
int count;
struct item *next;
}item;
typedef is used to create a shorthand notation for an existing type in C. It is similar to #define but unlike it, typedef is interpreted by the compiler and offers more advanced capabilities than the preprocessor.
With its simplest form, typedef is given as
typedef existing_type new_type;
for instance,
typedef unsigned long UnsignedLong;
For example, if you trace the definition of size_t back to its root, you will see that
/* sys/x86/include/_types.h in FreeBSD */
/* this is machine dependent */
#ifdef __LP64__
typedef unsigned long __uint64_t;
#else
__extension__
typedef unsigned long long __uint64_t;
#endif
...
...
typedef __uint64_t __size_t;
and then
/* stddef.h */
typedef __size_t size_t;
which actually means, size_t is an alias for unsigned long long,depending on the 64-bit modal (LP64, ILP64, LLP64) your machines has.
For your question, you attempt to define a new type but do not name it. Don't let the struct item {..} definition confuse you, it is just a type you are declaring. If you replace the whole struct item {...} with a basic type, say with an int, and rewrite your typedef, you would end up something like this
typedef int; /* new type name is missing */
the correct form should be
typedef struct item {...} Item;
See the examples below for different structure definitions
#include <stdio.h>
/* a new type, namely Item, is defined here */
typedef struct item_t {
char *text;
int count;
struct item_t *next; /* you canot use Item here! */
} Item;
/* a structure definition below */
struct item {
char *text;
int count;
struct item *next;
};
/* an anonymous struct
* However, you cannot self-refence here
*/
struct {
int i;
char c;
} anon;
int main(void) {
/* a pointer to an instance of struct item */
struct item *pi;
/* Shorthand for struct item_t *iI */
Item *iI;
/* anonymoous structure */
anon.i = 9;
anon.c = 'x';
return 0;
}
The typedef is useless because you didn't give it a name. You cannot use the typedef in any way. That's why you get a warning, because the typedef is useless.
The struct is actually still usable without the warning if you remove the typedef keyword like this:
struct item {
char *text;
int count;
struct item *next;
};
You just need to include the 'struct' keyword in the variable declaration. i.e.
struct item head;
as other have pointed out if you include the name at the end of the struct definition then you can use it as the typedef and you get rid of the warning even without the struct keyword but this makes the first instance of 'item' superfluous i.e.
typedef struct {
char *text;
int count;
struct item *next;
} item;
item head;
will also get rid of the warning.
This is an oldie, but stumbled here because I had the same linker error.
I made the foolish mistake of naming a file with an extension .c, and should have been .cpp - created the SAME error - so watch out!
Renamed the file to have a .cpp extension, and the problem magically went away.
Took a while to figure that out, so happy to share my experience.

Passing a struct item as the parameter

I understand it is possible to pass a struct as an argument etc.
But is it possible to have the parameter preset so only a specific struct item can be passed such:
struct inventory* searchForItem(struct stockItem.componentType code){
I'm getting : error: expected ';' , ', ' before token
EDIT:
typedef struct stockItem {
char *componentType;
char *stockCode;
int numOfItems;
int price;
} stockItem;
(Since the comments on the other answer are too constrained)
First you define a new type for componentType, like this:
typedef char *stockItem_componentType; // Naming subject to conventions
Now in your structure, you use this type instead of simple char*. This is optional, but very much recommended. Like this:
typedef struct stockItem {
stockItem_componentType componentType;
char *stockCode;
int numOfItems;
int price;
} stockItem;
And finally, your function prototype is:
struct inventory* searchForItem(stockItem_componentType code);
The type of that component is char *, so just make that the type of the parameter.
struct inventory* searchForItem(char *code){
If you want to make the type more strict, make a typedef for the field in question:
typedef char * stockItem_componentType;
typedef struct stockItem {
stockItem_componentType componentType;
char *stockCode;
int numOfItems;
int price;
} stockItem;
struct inventory* searchForItem(stockItem_componentType code){
Note that this hides a pointer behind a typedef which is not recommended. Then people reading your code (including yourself) won't know just by looking at it that it's a pointer, which can lead to confusion.

What this Declaration meant in C

I have a structure in C and at end have some declration which not able to decode
struct Student
{
int roll;
char name;
int age;
};
extern struct Student dev[];
what does last statement mean in C??
extern struct Student dev[];
Tells the compiler that dev is an array of the type struct Student and it is defined somewhere else(other translation unit).
It means that dev[] is not declared in this C/object file, but in another. You'll have to link that other object to your binary to be able to use that variable.
struct students
{
int num;
char name[100];
char dept[100];
} extern struct students student[];
student[] is the structure array.its used for the access the structure members
like num,name,dept.
int j=100;
#include<stdio.h>
main(){
for(i=0;i<j;i++)
{
scanf("%d",&student[i].num);
scanf("%s",student[i].name);
scanf("%s",student[i].dept);
}
for(i=0;i<j;i++)
{
printf("%d\n",student[i].num);
printf("%s\n",student[i].name);
printf("%s\n",student[i].dept);
}
}
It is used to the access the 100 records of members of the structure

Resources