like this ?

struct  {
    const char *s;
    double f;
} obj[] = {
    { .s = "ejf09j290fj390j2f09", .f = 0 },
    { .s = "dj320992209920209dj", .f = 0 }
};
Answer from BLUEPIXY on Stack Overflow
Top answer
1 of 2
4

like this ?

struct  {
    const char *s;
    double f;
} obj[] = {
    { .s = "ejf09j290fj390j2f09", .f = 0 },
    { .s = "dj320992209920209dj", .f = 0 }
};
2 of 2
1

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]. 256 is 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.

🌐
GeeksforGeeks
geeksforgeeks.org › c++ › array-of-objects-in-c-with-examples
Array of Objects in C++ with Examples - GeeksforGeeks
August 8, 2026 - ClassName is the name of the class. ... The Array of Objects stores objects.
Discussions

How to Create an array of objects? - C++ Forum
In array all objects are constructed in time of array declaration. That means you either should provide 20 initializers for your array (see exampe with 3 in my post earlier) or provide a default constructor and assign other values later. You might consider using vector instead. More on cplusplus.com
🌐 cplusplus.com
How to create an array of objects that holds objects of two classes derived from a single class? C++
https://stackoverflow.com/questions/2764671/creating-an-array-which-can-hold-objects-of-different-classes-in-c More on reddit.com
🌐 r/learnprogramming
1
2
May 7, 2022
Array of objects questions. - C++ Forum
Array of objects questions. ... I am very confused about array of objects coding. I have looked around a bit, and nothing of what I found was helpful in having me actually understand it (cause most of it was just all code and no words). So I don't know how to work an array of objects for data ... More on cplusplus.com
🌐 cplusplus.com
[C++] Array of objects
To answer your question, it would be Zombie** horde = new Zombie*[n]; , but... Are you sure you want an array like this? First of all, you are using new/delete instead of something a bit more modern and you are just returning a pointer, it smells a bit, how about a vector? std::vector zombieHorde(int n, std::string name) { // possibly sanitize n here (n cannot be negative) std::vector horde; horde.reserve(n); for(int i = 0; i < n; i++) { horde.emplace_back(name); // assuming the zombies are actually different } return horde; } More on reddit.com
🌐 r/AskProgramming
19
1
January 19, 2023
🌐
Medium
medium.com › @amalpp42 › array-of-objects-in-c-0169c01c7ebb
Array of objects in C++ | by Amalpp | Medium
August 2, 2024 - In C++, an array of objects is a collection of instances of a class or struct, stored contiguously in memory, allowing for efficient access and manipulation. Arrays of objects can be created similarly to arrays of fundamental data types.
🌐
Tutorjoes
tutorjoes.in › c_programming_tutorial › array_of_objects_in_c
Creating an Array of Structures in C
You can access the elements of the array using the array · index, and access the members of the structure using the ... //Array of Structure Objects #include<stdio.h> struct student { char *name; int age; float per; }; int main() { struct student o[2]; o[0].name="Ram Kumar"; o[0].age=25; o[0].per=65.25; o[1].name="Sam Kumar"; o[1].age=12; o[1].per=80; printf("\n------------------------------"); printf("\nName : %s",o[0].name); printf("\nAge : %d",o[0].age); printf("\nPercent : %f",o[0].per); printf("\n------------------------------"); printf("\nName : %s",o[1].name); printf("\nAge : %d",o[1].age); printf("\nPercent : %f",o[1].per); printf("\n------------------------------\n\n"); return 0; } To download raw file Click Here
🌐
Cplusplus
cplusplus.com › forum › beginner › 171111
How to Create an array of objects? - C++ Forum
In array all objects are constructed in time of array declaration. That means you either should provide 20 initializers for your array (see exampe with 3 in my post earlier) or provide a default constructor and assign other values later. You might consider using vector instead.
🌐
Quora
quora.com › How-do-I-create-an-array-of-objects-in-C++
How to create an array of objects in C++ - Quora
Answer (1 of 4): Oh god please never again call something else than an iterating integer 'i'. Give descriptive names, like inside the classes. One practice for constructors is the java style this-call: student(string name, string id, double balance) { this->name=name; this->id...
🌐
Reddit
reddit.com › r/learnprogramming › how to create an array of objects that holds objects of two classes derived from a single class? c++
r/learnprogramming on Reddit: How to create an array of objects that holds objects of two classes derived from a single class? C++
May 7, 2022 -

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.

Find elsewhere
🌐
Scribd
scribd.com › document › 891438492 › An-Array-of-Objects-in-C
An Array of Objects in C | PDF | Object Oriented Programming | C++
It covers fundamental aspects of object-orie…Full description ... The document explains the concept of arrays of objects in C++, detailing their declaration, initialization, and various methods for manipulation. It covers fundamental aspects of object-oriented programming, including the definition of objects and their relationship with arrays, alongside code examples for practical understanding.
🌐
Cplusplus
cplusplus.com › forum › beginner › 42815
Array of objects questions. - C++ Forum
Anything anyone could tell me that can help me understand this would be greatly appreciated. ... An array is simply a list of objects stored consecutively. Each object can be accessed by its position in the array. char arr[] = { 'a', 'b', 'c', 'd' }; This is an array of the first four letters ...
🌐
Sololearn
sololearn.com › en › Discuss › 981210 › how-can-i-create-array-of-object-in-c-assuming-i-have-a-class-students-which-each-of-my-object-will-instantiate-from
How can I create array of object in c++ assuming I have a ...
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
codezone online blog
codezone.blog › home › programing › array of objects in c++: a beginner’s guide you’ll actually understand
Array of Objects in C++: A Beginner's Guide You'll Actually Understand - codezone online blog
March 25, 2026 - Now let us get our hands dirty with code. We will start from the simplest example and progressively build toward more advanced patterns. Every array of objects begins with a well-defined class.
🌐
Learn C++ Online
learncpponline.com › home › concepts of classes and objects in c++ › array of objects in c++
Array of Objects in C++ - Learn C++ Online
September 19, 2015 - Similarly, the foremen array contains 15 objects (foremen) and the workers array contains 75 objects (workers). Since, an array of objects behave like any other array, we can use the usual array-accessing methods to access individual elements and then the dot member operator to access the member functions.
🌐
Virginia Tech
people.cs.vt.edu › kafura › cs2704 › arrays.html
Declaring Arrays of Objects
July 23, 1996 - The windows are the same shape and are aligned horizontally with the vertical edges of adjacent windows touching. from left to right, each window has one of the numbers from 1 to 10 written in it. Each "wave" beings with the leftmost window moving up vertically and then back to its original position. This same action is repeated for each window from left to right. Repeat this for a few waves. In this version declare the array so that the constructor with no arguments is used. Modify the "Frame Wave" program so that each object in the array is constructed by giving values for each constructor argument.
🌐
Reddit
reddit.com › r/askprogramming › [c++] array of objects
r/AskProgramming on Reddit: [C++] Array of objects
January 19, 2023 -

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?

🌐
Cplusplus
cplusplus.com › forum › general › 160923
Array of Objects in Class - C++ Forum
March 31, 2015 - so now i suppose the question would have been "how do you create an array of objects in a class where the objects are defined in another file?" }//end humor but by testing and ripping apart old code, i came up with this (marked below, in comments, are references for two topics): ***************************************** Vehicle.h
🌐
Cplusplus
cplusplus.com › forum › beginner › 19518
Arrays or Objects? - C++ Forum
Arrays and objects are orthogonal, but I understand what you are trying to ask. You are right in that it makes more sense to aggregate all the characteristics into a single class and then make an array of classes.
🌐
Unstop
unstop.com › home › blog › array of objects in c++ | declare & initialise (+ examples)
Array Of Objects In C++ | Declare & Initialise (+ Examples)
June 18, 2025 - Internships Jobs Compete Mentorship Courses Practice ... An array of objects in C++ is just like an array, but its elements are objects of a class.
🌐
Scribd
scribd.com › document › 456358080 › Array-of-Objects-in-c
Creating Arrays of Objects in C++ | PDF | Array Data Type | C++
If you suspect this is your content, claim it here. ... 1) An array of objects is created by declaring an array of a class type, where each element of the array is an object of that class.
Rating: 5 ​ - ​ 1 votes
🌐
Quora
quora.com › How-do-you-store-a-class-object-in-an-array-in-C
How to store a class object in an array in C++ - Quora
Answer (1 of 2): It is very simple. If you need to create 20 objects of a singular class Type; MyClass classname[20]; It is an array of type MyClass (which is your class). Unfortunately, there is not a way that I know of storing multiple types in an array. Such as different classes in an arr...