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);
    }
3
  • 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. Commented Jul 12, 2014 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 stackoverflow.com/questions/18308941/… Commented Jul 12, 2014 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. Commented Jul 12, 2014 at 18:41

4 Answers 4

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));
  }
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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);
}

2 Comments

can i set value of code[] inside if because it will be many value base on condition
Are the codes pre-determined, or are you calculating them?
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.

Comments

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.