http://libraryofcprograms.blogspot.com/2013/03/initializing-character-array-structs.html
Is there any other way to initialize a chararcter type array in structs?

Recommended Answers

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 << "var2.id = " << var2.id …
Jump to Post

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.

Jump to Post

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 << "var2.id = " << var2.id << 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.

Thanks!

Be a part of the DaniWeb community

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