# Define constant integer with if condition in c++

**URL:** <https://root-forum.cern.ch/t/define-constant-integer-with-if-condition-in-c/56862>\
**Category:** Newbie\
**Tags:** root\
**Created:** [October 26, 2023, 4:27am UTC](https://root-forum.cern.ch/t/define-constant-integer-with-if-condition-in-c/56862 "2023-10-26T04:27:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sawan4](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sawan4/32/35673_2.png) [@sawan4](https://root-forum.cern.ch/u/sawan4)\
**Post date:** [October 26, 2023, 4:27am UTC](https://root-forum.cern.ch/t/define-constant-integer-with-if-condition-in-c/56862/1 "2023-10-26T04:27:59Z")

</div>

To use a constant integer with the if condition I am using this logic in c++ code which compile with the g++ but not with root compiler.

```auto
#include <iostream>
using namespace std;

int main() {
     int wafer = 26;
    int nof1;

    if (wafer == 26)
    {
        nof1 = 25;
    }
    if (wafer == 10)
    {
        nof1 = 40;
    }
     const int nof = nof1;

     int array[nof] = {0};
     return 0;
}

```

In the root compilation I am getting this error, /home/sawan/FoCal\_India/position\_scan\_pi/check.cxx:18:16: error: variable-sized object may not be initialized  
int array[nof] = {0};  
^~~

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 26, 2023, 6:52am UTC](https://root-forum.cern.ch/t/define-constant-integer-with-if-condition-in-c/56862/2 "2023-10-26T06:52:24Z")

</div>

may be:

```auto
   int array[nof];
   for (int i=0; i<nof; i++) array[i] = 0;

```

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [October 26, 2023, 8:12am UTC](https://root-forum.cern.ch/t/define-constant-integer-with-if-condition-in-c/56862/3 "2023-10-26T08:12:28Z")

</div>

Hi,

instead of

```auto
int array[nof] = {0};

```

do

```auto
int array[nof];
memset(array, 0, sizeof(array));

```

---

<div class="post-metadata">

**Author:** ![sawan4](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sawan4/32/35673_2.png) [@sawan4](https://root-forum.cern.ch/u/sawan4)\
**Post date:** [October 26, 2023, 9:20am UTC](https://root-forum.cern.ch/t/define-constant-integer-with-if-condition-in-c/56862/4 "2023-10-26T09:20:32Z")

</div>

I have many arrays, histograms and graphs in to code whose size will be given by nof. So does this method works for histograms also? Like TH1F \*histograms[nof];

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [October 26, 2023, 9:33am UTC](https://root-forum.cern.ch/t/define-constant-integer-with-if-condition-in-c/56862/5 "2023-10-26T09:33:30Z")

</div>

You mean you have arrays of histograms and arrays of graphs? Why would you need to nullify the elements of such arrays?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 26, 2023, 9:41am UTC](https://root-forum.cern.ch/t/define-constant-integer-with-if-condition-in-c/56862/6 "2023-10-26T09:41:25Z")

</div>

You do not need to initialize the array to 0. Example:

```auto
void sawan() {
   int wafer = 26;
   int nof1;

   if (wafer == 26) nof1 = 25;
   if (wafer == 10) nof1 = 40;

   const int nof = nof1;

   int array[nof];
   memset(array, 0, sizeof(array));

   TH1F *histograms[nof];

   auto c = new TCanvas("c","c",1000,1000);
   c->Divide(5,5);
   for (int i=1; i<=nof; i++) {
      histograms[i-1] = new TH1F(Form("h%d",i),Form("h%d",i),100, -4,4);
      histograms[i-1]->FillRandom("gaus",20000);
      c->cd(i);
      histograms[i-1]->Draw();
   }
}

```

---

<div class="post-metadata">

**Author:** ![ctacke](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ctacke/32/32430_2.png) [@ctacke](https://root-forum.cern.ch/u/ctacke)\
**Post date:** [October 27, 2023, 5:34pm UTC](https://root-forum.cern.ch/t/define-constant-integer-with-if-condition-in-c/56862/7 "2023-10-27T17:34:54Z")

</div>

What about [`std::array`](https://en.cppreference.com/w/cpp/container/array)?

```cpp
#include <array>

void f()
{
    constexpr int a = 20;
    constexpr int b = (a == 20 ? 30 : 40);

    std::array<int, b> ar;
    ar.fill(0);
}

```

---

<div class="post-metadata">

**Author:** ![sawan4](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sawan4/32/35673_2.png) [@sawan4](https://root-forum.cern.ch/u/sawan4)\
**Post date:** [October 28, 2023, 4:32am UTC](https://root-forum.cern.ch/t/define-constant-integer-with-if-condition-in-c/56862/8 "2023-10-28T04:32:16Z")

</div>

For arrays, I am only filling the array at certain position and rest of the position I need to fill it with zero. This is why I was initializing them with 0;
