This question already has answers here:
How to initialize all members of an array to the same value?
(26 answers)
Closed 8 years ago.
My struct is as follow.
typedef struct user {
char *name;
int age;
} User;
Now when I am creating an array, I wonder what is the different between below both statments.
User users[10];
vs
User users[10] = {};
The first (User users[10];) do not erase the flash or memory so it is possibel that you have things on this memory from the Programm befor
Related
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;
};
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))
This question already has answers here:
How to copy a char array in C?
(14 answers)
C array declaration and assignment?
(7 answers)
Closed 5 years ago.
I get this error error: assignment to expression with array typ when I try this:
clients[0].data->bottle = x->bottle;
This is my code:
typedef struct {
int id;
some_data x;
uint64_t weight;
some_other_data_t *data;
} CLIENTS;
typedef struct {
uint8_t bottle[10];
} some_other_data_t;
some_other_data_t x;
extern CLIENTS clients[5];
/* some functions that set x correctly... */
clients[0].id = 1; // works fine!
clients[0].data->bottle = x->bottle; // does not work :(
Why is it so easy to set the id but so hard to set the data ?
I even tried to cast data to char* and use strcpy to set data but it doesn't work...
How can I set data?
I googled a lot but I didnt find a solution...
This question already has answers here:
How can you make a safe static singleton in Rust?
(3 answers)
How do I create a global, mutable singleton?
(7 answers)
Closed 5 years ago.
I need a "default", de facto an empty struct that I will be returning as a default value. Since it is read only, I don't want to pollute the memory by creating it N times, but rather return it only once
#[derive(Debug,PartialEq)]
pub struct Vocabulary {
literal_names: Vec<String>,
symbolic_names: Vec<String>,
display_names: Vec<String>,
max_toke_type: usize,
}
static EMPTY_VOCABULARY:Vocabulary = Vocabulary{
literal_names: Vec::new(),
symbolic_names: Vec::new(),
display_names: Vec::new(),
max_toke_type: 0 ,
};
This fails, considering function calls are not allowed in static context. How can I initialized these fields then?
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.