Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
跟踪满足C中某些要求的数组索引_C_Arrays_Avr Gcc - Fatal编程技术网

跟踪满足C中某些要求的数组索引

跟踪满足C中某些要求的数组索引,c,arrays,avr-gcc,C,Arrays,Avr Gcc,这种情况经常出现。在数组中循环,如果某些元素满足某些要求,则希望跟踪它们的索引以备将来使用。我的意思是: for(i=0;i<10;++i) { if(array[i] > 10) { //Keep track of this index for later use. } } for(i=0;i 10) { //跟踪此索引以备将来使用。 } } 简单的解决方案是创建一个由10个元素组成的数组,如果第二个元素大于10,则可以做索引[

这种情况经常出现。在数组中循环,如果某些元素满足某些要求,则希望跟踪它们的索引以备将来使用。我的意思是:

for(i=0;i<10;++i)
{
     if(array[i] > 10)
     {
          //Keep track of this index for later use.
     }
}
for(i=0;i 10)
{
//跟踪此索引以备将来使用。
}
}
简单的解决方案是创建一个由10个元素组成的数组,如果第二个元素大于10,则可以做索引[i]=1;但我觉得这种方法不太好。我需要一个大数组来存储这个,大部分空间都被浪费了

在我的应用程序中,我试图找出位数组中设置了哪些位。因此,如果设置了位0和位10,我需要存储这些数字以供程序以后使用。最好的办法是什么


这段代码需要在AVR Mega上运行,我使用的是AVR-GCC,因此需要一个只使用C的解决方案。

如果您觉得使用额外的数组来记住“特殊”索引会浪费很多空间,请尝试确定到底会浪费多少空间。然后,使用较小的数组。例如,如果您知道必须记住最多4个索引,请声明一个大小为4的数组

您还可以声明一个小数组,该数组的大小不足以记住所有索引,并运行多次填充该数组的循环:

int indices[4];
int number_of_indices = 0;
int i_start = 0; // array entries up to this index were already checked
while (i_start < 10) {
    for(i=i_start;i<10;++i)
    {
        if(array[i] > 10)
        {
            //Keep track of this index for "later use" below.
            indices[number_of_indices++] = i;
            // If 4 indices have been gathered, break the loop and use them
            if (number_of_indices == 4)
            {
                break;
            }
        }
    }
    i_start = i;

    // Put "Later use" here :)
    // Do something for the list of indices gathered so far
}
int索引[4];
指数的整数=0;
int i_start=0;//已检查此索引之前的数组项
同时(i_开始<10){
用于(i=i_开始;i 10)
{
//跟踪此索引,以供下文“以后使用”。
指数[指数的数量+]=i;
//如果已经收集了4个索引,则中断循环并使用它们
if(指数的数量=4)
{
打破
}
}
}
i_start=i;
//将“以后使用”放在此处:)
//对目前收集的索引列表做些什么
}

如果您觉得使用额外的数组来记住“特殊”索引会浪费很多空间,请尝试确定到底会浪费多少空间。然后,使用较小的数组。例如,如果您知道必须记住最多4个索引,请声明一个大小为4的数组

您还可以声明一个小数组,该数组的大小不足以记住所有索引,并运行多次填充该数组的循环:

int indices[4];
int number_of_indices = 0;
int i_start = 0; // array entries up to this index were already checked
while (i_start < 10) {
    for(i=i_start;i<10;++i)
    {
        if(array[i] > 10)
        {
            //Keep track of this index for "later use" below.
            indices[number_of_indices++] = i;
            // If 4 indices have been gathered, break the loop and use them
            if (number_of_indices == 4)
            {
                break;
            }
        }
    }
    i_start = i;

    // Put "Later use" here :)
    // Do something for the list of indices gathered so far
}
int索引[4];
指数的整数=0;
int i_start=0;//已检查此索引之前的数组项
同时(i_开始<10){
用于(i=i_开始;i 10)
{
//跟踪此索引,以供下文“以后使用”。
指数[指数的数量+]=i;
//如果已经收集了4个索引,则中断循环并使用它们
if(指数的数量=4)
{
打破
}
}
}
i_start=i;
//将“以后使用”放在此处:)
//对目前收集的索引列表做些什么
}

您可以使用位图:每个索引只使用1位,而不是每个索引使用16或32位

uint32_t bitmap[10] = {0}; // works for array size up to 320 elements
for(i=0;i<10;++i)
{
     if(array[i] > 10)
     {
          //Keep track of this index for later use.
          bitmap[i/32] |= (uint32_t)1 << (i%32);
     }
}

for(i=0;i<10;++i)
{
     if((bitmap[i/32] >> (i%32)) & 1)
     {
         // Later use :)
         // Put some code here
     }
}
uint32\u t位图[10]={0};//适用于最多320个元素的阵列大小
对于(i=0;i=10)
{
//跟踪此索引以备将来使用。
位图[i/32]|=(uint32_t)1(i%32))&1)
{
//以后使用:)
//在这里输入一些代码
}
}

您可以使用位图:每个索引只使用1位,而不是每个索引使用16或32位

uint32_t bitmap[10] = {0}; // works for array size up to 320 elements
for(i=0;i<10;++i)
{
     if(array[i] > 10)
     {
          //Keep track of this index for later use.
          bitmap[i/32] |= (uint32_t)1 << (i%32);
     }
}

for(i=0;i<10;++i)
{
     if((bitmap[i/32] >> (i%32)) & 1)
     {
         // Later use :)
         // Put some code here
     }
}
uint32\u t位图[10]={0};//适用于最多320个元素的阵列大小
对于(i=0;i=10)
{
//跟踪此索引以备将来使用。
位图[i/32]|=(uint32_t)1(i%32))&1)
{
//以后使用:)
//在这里输入一些代码
}
}

在PC上,我认为动态增长的链表或堆栈最好


在微控制器上,通常最好使用静态分配的结构,以便性能具有确定性,并避免浪费宝贵的内存。因此,一个固定大小的FIFO存储您关心的索引(而不是一个简单的1/0状态)是最好的选择。只要准备好在出现溢出时考虑检测和故障,或者找到某种方法来保证没有溢出。

在PC上,我认为动态增长的链表或堆栈是最好的


在微控制器上,通常最好使用静态分配的结构,以便性能具有确定性,并避免浪费宝贵的内存。因此,一个固定大小的FIFO存储您关心的索引(而不是一个简单的1/0状态)是最好的选择。只要准备好在溢出时考虑检测和故障,或者找到某种方法保证不溢出。

为什么不在第一次遇到它时处理每一个案例?因为其他数据可能还没有被微控制器读入。为什么不在第一次遇到它时处理每一个案例?因为其他数据可能没有已经被微控制器读入了。很好。谈论低内存使用率。虽然我看到你的第一个答案和我的想法是一样的,很好。谈论低内存使用率。虽然我看到你的第一个答案和我的想法是一样的。