like this ?
Copystruct {
const char *s;
double f;
} obj[] = {
{ .s = "ejf09j290fj390j2f09", .f = 0 },
{ .s = "dj320992209920209dj", .f = 0 }
};
Answer from BLUEPIXY on Stack Overflowlike this ?
Copystruct {
const char *s;
double f;
} obj[] = {
{ .s = "ejf09j290fj390j2f09", .f = 0 },
{ .s = "dj320992209920209dj", .f = 0 }
};
Where JavaScript has dynamic "properties", C has static "tags". A struct type is defined as a sequence of tags, which each have a type.
For the type inside your example array
CopyarrayOfObjectsInsideJavascript = [
{ s:"ejf09j290fj390j2f09", f=0 },
{ s:"dj320992209920209dj", f=0 }
]
your last solution started off almost correctly:
Copystruct element {
char s[256];
int f;
};
You have two options for the type of s:
- If you want to store the string inside the object, you use some array type, such as
char s[256].256is the length of the array, so when you use zero-terminated strings, the maximum allowed string length is 255 (which is 256, minus one for the'\0'character). - If you want to store the string outside the object, you use some pointer type. Since you want to use string literals, you should declare the tag as
const char *s. Changing a string literal causes undefined behaviour, so it's best to prevent yourself from doing so.
For this answer, I'll use the first option in the examples.
If you want to define one struct, you could now write something like
Copystruct element {
char s[256];
int f;
} one_object = {
.s = "ejf09j290fj390j2f09",
.f = 0,
};
or
Copystruct element {
char s[256];
int f;
};
/* ... later, in a scope where `struct element` is visible ... */
struct element one_object = {
.s = "ejf09j290fj390j2f09",
.f = 0,
};
or, with a typedef,
Copytypedef struct {
char s[256];
int f;
} your_element_type;
/* ... later, in a scope where `your_element_type` is visible ... */
your_element_type one_object = {
.s = "ejf09j290fj390j2f09",
.f = 0,
};
Note that this doesn't necessarily work with older compilers that don't support the C99 standard. In the older days, you'd have to initialise fields in order:
Copyyour_element_type one_object = { "ejf09j290fj390j2f09", 0 };
Also note that, if you're never going to refer to the type name struct element again, you don't have to name it:
Copystruct {
char s[256];
int f;
} one_object = {
.s = "ejf09j290fj390j2f09",
.f = 0,
};
Arrays are initialised similarly (you've actually already initialised an array, namely s, with a string):
Copystruct element {
char s[256];
int f;
} so_many_objects[] = {
{ .s = "ejf09j290fj390j2f09", .f = 0 },
{ .s = "dj320992209920209dj", .f = 0 },
};
or
Copystruct element {
char s[256];
int f;
};
/* ... later, in a scope where `struct element` is visible ... */
struct element so_many_objects[] = {
{ .s = "ejf09j290fj390j2f09", .f = 0 },
{ .s = "dj320992209920209dj", .f = 0 },
};
or, with a typedef again,
Copytypedef struct {
char s[256];
int f;
} your_element_type;
/* ... later, in a scope where `your_element_type` is visible ... */
your_element_type so_many_objects[] = {
{ .s = "ejf09j290fj390j2f09", .f = 0 },
{ .s = "dj320992209920209dj", .f = 0 },
};
By the way, note that we are using an incomplete type for so_many_objects; this is only possible because the compiler can see how big the array should be. In these examples, the compiler would treat your code as if you'd have written struct element so_many_objects[2]. If you think you'll have to extend the array with more objects, you'd better specify how many elements the array has (at maximum), or learn about dynamic allocation.
How to Create an array of objects? - C++ Forum
Array of objects questions. - C++ Forum
How to create an array of objects from classes?
array of objects
Videos
So there's this base class and two other classes have been derived from it. How does one create an array that holds objects of both derived classes?
EDIT: I did managed to achieve this by creating an array of pointers of type base i.e Base * arr[10]; and stored the addresses of the objects of the derived classes inside it.
Like, instead of making : zombie zom1 = new zombie() zombie zom2 = new zombie() zombie zom3 = new zombie() And so on, I want to instead make something like: zombie[] zomb = new zombie[88] And randomly choose a zombie from the 88 to do an action, like: zomb[R].shriek() Where R is a random number
Good day,
So I created a structure Position with two int members.
struct Position {
int x;
int y;
};
typedef struct Position Position;
int main() {
Position *current = (Position*)malloc(sizeof(Position));
...
}If I want to create an array of it, should I just multiply it to how many elements I want to use?Does pointer arithmetic will move me to the next element struct every time I add 1 to the pointer?Why when I try to reallocate it by multiplying to higher number, it returns invalid pointer even though I just passed the pointer coming from malloc(). Thanks for answering and have a nice day.