0

Can I do somethings like this in c++ or what should I do.

unsigned int code[180];

    if ( somethings ) 
    {
      code[] = {3150,1550,400,400,400,1150,450,350,400,400,400,1200,400,400,400,1150,400,400,400,400,400,1150,400,1200,400,1200,400,350,400,1200,400,400,400,1150,400,1200,400,1200,400,350,400,400,400,400,400,400,400,1150,400,1200,400,400,400,1150,400,1200,400,400,400,400,400,1150,400,400,400,400,400,1150,450,350,400,400,400,1200,400,1150,400,400,400,1200,400,1150,400,1200,400,1150,450,1150,400,1200,400,350,450,1150,400,400,400,1150,450,350,400,400,400,400,400,400,400,1150,400,400,400,1200,400,400,400,1150,400,1200,400,1150,400,1200,400,1200,400,350,400,400,400,1200,400,400,400,350,400,400,400,400,400,400,400,1150,400,1200,400,400,400,400,400,1150,400,1200,400,1150,450,1150,400,400,350,1200,450,350,400,1200,400,400,400,350,450,350,400,400,400,1200,400,350,450,1150,400,1000};
      //irsend.sendRaw(code,sizeof(code)/sizeof(int),khz);
    }
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Jongz Puangput
  • 5,527
  • 10
  • 58
  • 96
  • 7
    You can use a `std::array` or a `std::vector` for the array, solves the assignment problem. You can use `std::string` for the string, solves the comparison problem. – Cheers and hth. - Alf Jul 12 '14 at 18:20
  • I guess you need to test it by yourself (create a new file and compile it), or dig through the C++ standard http://stackoverflow.com/questions/18308941/c-array-zero-initialization-is-this-a-bug-or-is-this-correct – Ming-Tang Jul 12 '14 at 18:20
  • If you must use C-style code, create global/static array and assign its address to pointer. If you want a copy, use `std::memcpy`. But, prefer C++ containers mentioned in a comment above. – hyde Jul 12 '14 at 18:41

4 Answers4

1

I don't think you can initialize the array using {...} after it's declaration. So, use a temporary array to store your contents and memcpy the contents into it (if that is your use case).

unsigned int code[180];
  if ( _SOME_CONDITION_ ) 
  {      

    unsigned int temp1 [] ={3150,1550,400,400,400,1150,450,350,400,400,400,1200,400,400,400,1150,400,400,400,400,400,1150,400,1200,400,1200,400,350,400,1200,400,400,400,1150,400,1200,400,1200,400,350,400,400,400,400,400,400,400,1150,400,1200,400,400,400,1150,400,1200,400,400,400,400,400,1150,400,400,400,400,400,1150,450,350,400,400,400,1200,400,1150,400,400,400,1200,400,1150,400,1200,400,1150,450,1150,400,1200,400,350,450,1150,400,400,400,1150,450,350,400,400,400,400,400,400,400,1150,400,400,400,1200,400,400,400,1150,400,1200,400,1150,400,1200,400,1200,400,350,400,400,400,1200,400,400,400,350,400,400,400,400,400,400,400,1150,400,1200,400,400,400,400,400,1150,400,1200,400,1150,450,1150,400,400,350,1200,450,350,400,1200,400,400,400,350,450,350,400,400,400,1200,400,350,45 0,1150,400,1000};
    //irsend.sendRaw(code,sizeof(code)/sizeof(int),khz);

    memcpy(code, temp1, sizeof(temp1) / sizeof(*temp1));
  } else {
    unsigned int temp2 = {...};
    memcpy(code, temp2, sizeof(temp2) / sizeof(temp2));
  }
K.K
  • 2,647
  • 1
  • 26
  • 32
1

since you tagged this c++, I would suggest:

vector<int> code;

if( condition_1 )
    code = { 3,7,8 };
else if( condition_2 )
    code = { 9,11,34 };

etc.

it requires c++11 to compile

sp2danny
  • 7,488
  • 3
  • 31
  • 53
0

Set the code ahead of time:

unsigned int code[] = {3150,1550,400,400,400,1150,450,350,400,400,400,1200,400,400,400,1150,400,400,400,400,400,1150,400,1200,400,1200,400,350,400,1200,400,400,400,1150,400,1200,400,1200,400,350,400,400,400,400,400,400,400,1150,400,1200,400,400,400,1150,400,1200,400,400,400,400,400,1150,400,400,400,400,400,1150,450,350,400,400,400,1200,400,1150,400,400,400,1200,400,1150,400,1200,400,1150,450,1150,400,1200,400,350,450,1150,400,400,400,1150,450,350,400,400,400,400,400,400,400,1150,400,400,400,1200,400,400,400,1150,400,1200,400,1150,400,1200,400,1200,400,350,400,400,400,1200,400,400,400,350,400,400,400,400,400,400,400,1150,400,1200,400,400,400,400,400,1150,400,1200,400,1150,450,1150,400,400,350,1200,450,350,400,1200,400,400,400,350,450,350,400,400,400,1200,400,350,450,1150,400,1000};

if (strcmp((char*)data,"off") == 0) 
{
    // do stuff with the code here
    //irsend.sendRaw(code,sizeof(code)/sizeof(int),khz);
}
Michael Gazonda
  • 2,720
  • 1
  • 17
  • 33
0

This

code[] = {3150,1550,400,400,400,1150,450,350,400,400,400,1200,400,400,400,1150,400,400,400,400,400,1150,400,1200,400,1200,400,350,400,1200,400,400,400,1150,400,1200,400,1200,400,350,400,400,400,400,400,400,400,1150,400,1200,400,400,400,1150,400,1200,400,400,400,400,400,1150,400,400,400,400,400,1150,450,350,400,400,400,1200,400,1150,400,400,400,1200,400,1150,400,1200,400,1150,450,1150,400,1200,400,350,450,1150,400,400,400,1150,450,350,400,400,400,400,400,400,400,1150,400,400,400,1200,400,400,400,1150,400,1200,400,1150,400,1200,400,1200,400,350,400,400,400,1200,400,400,400,350,400,400,400,400,400,400,400,1150,400,1200,400,400,400,400,400,1150,400,1200,400,1150,450,1150,400,400,350,1200,450,350,400,1200,400,400,400,350,450,350,400,400,400,1200,400,350,450,1150,400,1000};

would be an initialization and you can not do such thing in C++ after declaration. Proper way would be something like:

const int code_if[] = { /* your numbers here */ };
int code[];
if ( /* something */ ) {
    code = code_if;
}

If you really need to move in the array, then you should use memcpy() as others already suggested.

Jan Smrčina
  • 126
  • 2
  • 5