using a member const int to initialize a char array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • abubakarm@gmail.com

    #1

    using a member const int to initialize a char array

    Hi,

    If i have the following struct:

    struct t
    {
    const int x;// = 0;
    char name [ x ];

    };

    its gives an error saying that x is undefined. So I want to initialize
    x and use it to initalize other members. How do I do this? Or should I
    use some other way?

    Thanks,

    ...ab
  • Leandro Melo

    #2
    Re: using a member const int to initialize a char array

    On 14 ago, 11:45, abubak...@gmail .com wrote:
    Hi,
    >
    If i have the following struct:
    >
    struct t
    {
      const int x;// = 0;
      char name [ x ];
    >
    };
    >
    its gives an error saying that x is undefined. So I want to initialize
    x and use it to initalize other members. How do I do this? Or should I
    use some other way?
    >
    Thanks,
    >
    ..ab
    Hi.

    You can use the following (don't use 0 however):

    struct A
    {
    static const int x = 10;
    char name [ x ];

    };

    --
    Leandro Melo

    Comment

    • Victor Bazarov

      #3
      Re: using a member const int to initialize a char array

      Leandro Melo wrote:
      On 14 ago, 11:45, abubak...@gmail .com wrote:
      >Hi,
      >>
      >If i have the following struct:
      >>
      >struct t
      >{
      > const int x;// = 0;
      > char name [ x ];
      >>
      >};
      >>
      >its gives an error saying that x is undefined. So I want to initialize
      >x and use it to initalize other members. How do I do this? Or should I
      >use some other way?
      >>
      >Thanks,
      >>
      >..ab
      >
      Hi.
      >
      You can use the following (don't use 0 however):
      >
      struct A
      {
      static const int x = 10;
      char name [ x ];
      >
      };
      If the value of 'x' is dynamic (determined only in run-time), then you
      can't use this. You need to either use a dynamic array or a std::vector
      for your 'name':

      struct A
      {
      const int x;
      std::vector<cha rname;
      A(int x) : x(x), name(x) {}
      };

      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask

      Comment

      • abubakarm@gmail.com

        #4
        Re: using a member const int to initialize a char array

        On Aug 14, 8:33 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
        Leandro Melo wrote:
        On 14 ago, 11:45, abubak...@gmail .com wrote:
        Hi,
        >
        If i have the following struct:
        >
        struct t
        {
          const int x;// = 0;
          char name [ x ];
        >
        };
        >
        its gives an error saying that x is undefined. So I want to initialize
        x and use it to initalize other members. How do I do this? Or should I
        use some other way?
        >
        Thanks,
        >
        ..ab
        >
        Hi.
        >
        You can use the following (don't use 0 however):
        >
        struct A
        {
          static const int x = 10;
          char name [ x ];
        >
        };
        >
        If the value of 'x' is dynamic (determined only in run-time), then you
        can't use this.  You need to either use a dynamic array or a std::vector
        for your 'name':
        >
             struct A
             {
                 const int x;
                 std::vector<cha rname;
                 A(int x) : x(x), name(x) {}
             };
        >
        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask
        Thank you guys !

        ..ab

        Comment

        Working...