like this ?
struct {
const char *s;
double f;
} obj[] = {
{ .s = "ejf09j290fj390j2f09", .f = 0 },
{ .s = "dj320992209920209dj", .f = 0 }
};
Answer from BLUEPIXY on Stack Overflowlike this ?
struct {
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
arrayOfObjectsInsideJavascript = [
{ s:"ejf09j290fj390j2f09", f=0 },
{ s:"dj320992209920209dj", f=0 }
]
your last solution started off almost correctly:
struct 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
struct element {
char s[256];
int f;
} one_object = {
.s = "ejf09j290fj390j2f09",
.f = 0,
};
or
struct 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,
typedef 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:
your_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:
struct {
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):
struct element {
char s[256];
int f;
} so_many_objects[] = {
{ .s = "ejf09j290fj390j2f09", .f = 0 },
{ .s = "dj320992209920209dj", .f = 0 },
};
or
struct 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,
typedef 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
How to create an array of objects that holds objects of two classes derived from a single class? C++
Array of objects questions. - C++ Forum
[C++] Array of objects
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.
I tried asking this question a few different ways, but kept getting the terminology mixed up. I think it's easier with just an example.
I have this function. It should work (afaik):
Zombie* zombieHorde(int n, std::string name) {
Zombie* horde = new Zombie[n];
for(int i = 0; i < n; i++) {
horde[i].setName(name);
}
return horde;
}I want to change it to something like this. I have no idea what to put in place of the (3 sets of) question marks. These two functions should behave identically.
Zombie* zombieHorde(int n, std::string name) {
?? horde = ??;
for(int i = 0; i < n; i++) {
horde[i] = new Zombie(name);
}
return horde??;
}In other words, I want to make an array of objects using a non-default constructor. I want to declare an array of objects without actually instantiating those objects, at least not until a later step.
How do I do this?