Recursive struct [duplicate] - c

This question already has answers here:
self referential struct definition?
(9 answers)
Closed 9 years ago.
Do I need to use typedef in order to build recursive structs? I tried to use the following code without success:
struct teste
{
int data;
int data2;
struct teste to_teste;
};

To build recursive structs you do not need typedef.
You will have to convert the struct object into a struct pointer object.
like this:
struct teste{
int data;
int data2;
struct teste *to_teste;
};

You CANNOT have the same structure inside itself. If you do that then size of that structure becomes indefinite. So that is not allowed.
Instead you can have a pointer to the same structure inside itself to solve your purpose. This will work because size of a pointer is known to the compiler and the structure has a definite size now.

Related

Inter-nested struct type in C [duplicate]

This question already has answers here:
Structs that refer to each other
(4 answers)
Closed 2 years ago.
I want to create 2 structs that are nested inside each other, but C won't allow this as when I define the first struct, the second struct is still not defined. How to solve this?
code
what you need to do is add pointers to refer to the other structs :
struct y;
struct x {
struct y * yy;
};
struct y {
struct x * xx;
};

Defining a new instance of a struct [duplicate]

This question already has answers here:
Is it optional to use struct keyword before declaring a structure object?
(3 answers)
Closed 2 years ago.
I tried typing the following code in a simple C project, but it keeps saying that MyStruct is undefined – unless I add struct before every MyStruct (i.e. struct MyStruct my_struct; which just feels wrong?).
struct MyStruct {
int my_int;
}
int main() {
MyStruct my_struct;
my_struct->my_int = 1;
return 0;
}
It’s not wrong, it’s the way C works. Type name is struct MyStruct (it would be simply MyStruct in C++). If you feel that inconvenient, make a typedef, like:
typedef struct MyStruct { ... } MyStruct;
That may or may not be considered a good practice, though.
Also note that a struct (but not a typedef) and a function can have the same name (without the struct prefix). sigaction is a real-word example of that.

C structs: Can a function argument be used to deference a struct pointer [duplicate]

This question already has answers here:
How can I access structure fields by name at run time?
(5 answers)
Passing a struct member NAME to function in C?
(1 answer)
How to access members of a `struct' according to a value of a string?
(3 answers)
Closed 5 years ago.
I'm learning about OOP C and I want to write the equivalent of getters and setters.
I have the following struct.
typedef struct {
int item0;
int item1;
int item2;
etc...;
} ItemsStr;
typedef ItemsStr * ItemsPtr;
items_get_item(ItemsPtr item, itemname) {
return item->itemname;
}
I realize that the syntax is invalid but is there a way to accomplish the above?
I also realize that I could write a switch statement but I was wondering if there was a way to use the parameter passed in to deference.
You can use offsetof():
int items_get_item(ItemsPtr items, size_t offset) {
return *(int*)((char*)items + offset);
}
Then the caller does this:
items_get_item(items, offsetof(ItemsStr, item1))

C List: dealing with typedef [duplicate]

This question already has answers here:
How to define a typedef struct containing pointers to itself?
(2 answers)
Closed 9 years ago.
if there a way to write this code with a typedef for the struct?
struct s_parola{
char info[40];
struct s_parola *next;
};
I tried with this code but it isn't correct:
typedef struct {
char info[40];
t_parola *next;
} t_parola;
Thank you
If your problem is that you're receiving an "unknown type s_parola" within your struct, it's because you're trying to use the typedef before it's defined. You could do the following instead:
typedef struct s_parola s_parola;
struct s_parola {
char info[40];
s_parola *next;
};
This is what works best for me:
struct _s_parola {
char info[40];
struct _s_parola *next;
};
typedef struct _s_parola s_parola;
int main() {
s_parola p;
}
For using s_parola as a type inside s_parola you need to use struct. However this way you can use s_parola without struct anywhere else in your program.
Edit
#JamesReed 's answer works too, I'm just not comfortable using
typedef struct s_parola s_parola;
Might be 100% OK, but I am not comfortable. I am actually curious if that is standard compliant. If it is, it's a better solution than mine. If someone could answer would be great.

What's the aim of typedef a former name? [duplicate]

This question already has answers here:
typedef struct vs struct definitions [duplicate]
(12 answers)
Closed 10 years ago.
Such as:
typedef struct _cairo_clip cairo_clip_t;
Why not directly use _cairo_clip? See numerous similar definitions in some code.
The idea behind typedef is to let you skip the struct keyword. Unlike C++, C does not let you do this:
struct _cairo_clip {
int a;
float b;
};
_cairo_clip cc; // Not allowed
struct _cairo_clip cc; // Allowed, but requires a keyword
If you tried to directly use _cairo_clip, you would need to call it struct _cairo_clip.
struct _cairo_clip is more verbose than cairo_clip_t.
Using a typedef also provides some abstraction, because it means that cairo_clip_t can be implemented as either a built-in type or as implemented as a struct, without causing a change in the syntax of the client code.

Resources