0
#include<iostream>
#include<string>
using namespace std;
int main(void) {

    struct STRCT {
        int num;
        string str1,
               arrStr1[],
               str2,
               arrStr2[];
    };

    int a;
    string b[2],
           c[3],
           d,
           e;

    a = 10;

    b[0] = "hello";
    b[1] = "world";

    c[0] = "stack";
    c[1] = "over";
    c[2] = "flow";

    d = "random";
    e = "text";

    //how do i intialize the arrays (arrStr1[] and arrStr2[]) in aStruct along with the rest of items?
    //is it like this?

    //i want aStruct[] to be an array and i want its size to be declared from the start to later be filled with vals

    STRCT aStruct[2];

    //then later in the program i want to assign aStruct[] vals

    aStruct[0] = {a,      //int
                  d,      //string
                  {b},    //string[]
                  e,      //string
                  {c}};   //string[]
}

so basically i want to make a struct array with arrays inside then get the proper vals and then assign the proper vals to the arrays inside the struct array. thank you very much in advance for the help

0

2 Answers 2

3

Array declarations in your struct are simply illegal. C++ does not support size-less array declarations as class members. And even if some C++ compiler supports a C99-style "struct hack" declaration, only one size-less array is allowed and the array must be the last member of the struct.

You want to have arrays inside your struct - you have to give them specific compile-time sizes. Without specific compile time size you'll have to use pointers or std::vector.

In your example b has size 2 and c has size 3. You can declare your struct with the same sizes

struct STRCT {
  int num;
  string str1, arrStr1[2], str2, arrStr2[3];
};

and then initialize it as follows

STRCT aStruct[2] = 
{
  {
    a,
    d,
    { b[0], b[1] },
    e,
    { c[0], c[1], c[2] }
  }

  // The rest of the array is value-initialized
};

That's just as far as you can get with ordinary arrays. It you want something more flexible, embedding arrays straight into the struct won't help you here. Either construct the necessary memory structures manually or use std::vector.

Sign up to request clarification or add additional context in comments.

1 Comment

ok, but finally what would i do if i wanted arrStr1[]'s size to be equal to b[]'s size without knowing what b[]'s size is before hand lets say i declare the size of b[] by using a users input how would i then intialize arrStr1[(size of b)] inside the struct?
0

In C++ this is illegal

string arr[2] = {"This","is"};
string arr1[2];

arr1 = arr;

There is nothing like "copy an entire array into another array". The array elements must be copied individually.

Second you cant declare arrays of unknown size

You can modify your struct declaration by declaring string array of fixed size and do this

for(int i =0; i< 2; i++)
{

   aStruct[i].num = a;
   aStruct[i].str1= d;

   for(int j=0;j<2;j++)
   {
     arrStr1[i] = b[i];
   }

   aStruct[i].str2= e;

   for(int k=0;k<3;k++)
   {
     arrStr2[i] = c[i];
   }
}

I suggest instead of string arrStr1[] ,string arrStr2[] , b[2] and c[2] make use of std::vector. That will help you in avoid hard coding the conditions in for loop.

2 Comments

Thanks you for your reply, i will make use of vectors soon when i learn them next week. so theres no way to directly fill the arrStr1[] etc by a method similar to what i enlisted
ok, thank you very much for your help, i see now that the general thinking i had was just invalid, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.