稍后在{}中添加unsigned int数组

时间:2014-07-12 18:18:14

标签: c++

我可以在c ++中做这样的事情,或者我该怎么做。

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

4 个答案:

答案 0 :(得分:1)

我不认为你可以在声明后使用{...}初始化数组。因此,使用临时数组来存储您的内容并将内容存储到其中(如果这是您的用例)。

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

答案 1 :(得分:1)

因为你标记了这个c ++,我建议:

vector<int> code;

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

它需要c ++ 11来编译

答案 2 :(得分:0)

提前设置代码:

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

答案 3 :(得分:0)

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

是一个初始化,在声明后你不能在C ++中做这样的事情。 正确的方法是:

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

如果你真的需要移入数组,那么你应该像其他人已经建议的那样使用memcpy()。