Is there any other way to initialize a chararcter type array in structs?

Recommended Answers

All 6 Replies

Using the initializer list you can take advantage of initialization rules:

#include <iostream>

using namespace std;

struct a {
    double id;
    int tag;
    char array[10];

int main()
    a var1 = {100, 1, "The City"};
    a var2 = var1;

    cout << " = " << << endl;
    cout << "var2.tag = " << var2.tag << endl;
    cout << "var2.array = " << var2.array << endl;

Thanks, any other method?

What are you trying to accomplish? The answer to that is what determines how you initialize your structure.

I just wanted to know all the methods to initialize.

Ignoring variations of the following, you can initialize it manually member-by-member, copy it from an existing object, use an initializer list, or define a constructor.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.