【问题标题】:How to iterate over a array using a pointer and length unknown?如何使用指针和长度未知迭代数组?
【发布时间】:2012-04-16 07:16:22
【问题描述】:

我已经获得了可以使用的 c api 和最少的文档。 开发人员目前不在,他的代码返回了意外的值(数组不是预期的长度)

我遇到了返回数组指针的方法的问题,我想知道我是否正确地迭代它们。

问:以下是否总是返回正确的数组 len?

int len=sizeof(sampleState)/sizeof(short);
int len=sizeof(samplePosition)/sizeof(int);

 typedef unsigned char byte;
 int len=sizeof(volume)/sizeof(byte);

我使用指针和指针算术遍历数组(我对下面的所有类型都正确执行了吗)

下面的最后一个例子是多维数组?对此进行迭代的最佳方法是什么?

谢谢

//property sampleState returns short[] as short* 

    short* sampleState = mixerState->sampleState;
    if(sampleState != NULL){
        int len=sizeof(sampleState)/sizeof(short);
        printf("length of short* sampleState=%d\n", len);//OK

        for(int j=0;j<len;j++) {
            printf("    sampleState[%d]=%u\n",j, *(sampleState+j));                
        }
    }else{
        printf("    sampleState is NULL\n"); 
    }

//same with int[] returned as  int*     

    int* samplePosition = mixerState->samplePosition;
    if(samplePosition != NULL){
        int len=sizeof(samplePosition)/sizeof(int);
        printf("length of int* samplePosition=%d\n", len);//OK

        for(int j=0;j<len;j++) {
            printf("    samplePosition[%d]=%d\n",j, *(samplePosition+j));                
        }
    }else{
        printf("    samplePosition is NULL\n"); 
    }

这里的字节是类型def到

typedef unsigned char byte;

所以我用了 %u

    //--------------
    byte* volume    = mixerState->volume;

    if(volume != NULL){
        int len=sizeof(volume)/sizeof(byte);
        printf("length of [byte* volume = mixerState->volume]=%d\n", len);//OK

        for(int j=0;j<len;j++) {
            printf("    volume[%d]=%u\n",j, *(volume+j));                
        }
    }else{
        printf("    volume is NULL\n"); 
    }

这里是int[][] soundFXStatus

我是否只是使用上述相同的方法并有 2 个循环?

    //--------------
    int** soundFXStatus         = mixerState->soundFXStatus;

【问题讨论】:

  • 您标记了两种不同的编程语言。正确答案会有所不同,具体取决于您实际指的是哪种语言。你想要 C++ 还是 C 的答案?

标签: c++ c arrays pointers multidimensional-array


【解决方案1】:

sizeof(array)/sizeof(element) 技巧只有在你有一个实际数组而不是指针时才有效。如果你只有一个指针,没有办法知道数组的大小。您必须将数组长度传递给函数。

或者最好使用vector,它具有size 函数。

【讨论】:

    【解决方案2】:
    sizeof(sampleState)/sizeof(short);
    

    这将sampleState 被声明为数组而不是指针时给出数组的长度:

    short array[42];
    sizeof(array)/sizeof(short);    // GOOD: gives the size of the array
    sizeof(array)/sizeof(array[0]); // BETTER: still correct if the type changes
    
    short * pointer = whatever();
    sizeof(pointer)/sizeof(short);  // BAD: gives a useless value
    

    另外,请注意函数参数实际上是指针,即使它看起来像一个数组:

    void f(short pointer[]) // equivalent to "short * pointer"
    {
        sizeof(pointer)/sizeof(short); // BAD: gives a useless value
    }
    

    在您的代码中,sampleState 是一个指针;仅给定一个指向数组的指针,就无法确定数组的长度。大概 API 提供了一些获取长度的方法(否则它将无法使用),您需要使用它。

    在 C++ 中,这就是为什么您更喜欢 std::vectorstd::array 而不是手动分配的数组的原因之一;尽管这对您没有帮助,因为尽管有问题标签,但您在这里使用的是 C。

    【讨论】:

      【解决方案3】:
      int len=sizeof(sampleState)/sizeof(short);
      int len=sizeof(samplePosition)/sizeof(int);
      

      sizeof 在编译时完成,因此如果在编译时不知道数组的长度(例如使用 malloc 保留内存),则此方法不起作用。

      【讨论】:

      • 编译时间,不是预编译时间。
      • 不一定在编译时 - sizeof 在运行时为 C99 可变长度数组完成。
      • 我犯了一个错误,“p.ex”没有任何意义。我应该使用例如(例如)。
      【解决方案4】:

      好吧,忽略我上面使用的方法,这都是错误的——尽管你确实需要知道我最终从 API 开发人员那里得到的数组的长度。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-09
        • 2014-04-22
        • 2019-08-24
        • 1970-01-01
        • 2017-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多