【发布时间】: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