C++ 如何使用长度未知的指针遍历数组?
我已经得到了一个c api和最低限度的文档。 开发人员目前不在,他的代码返回了意外的值(数组的长度不是预期的) 我对返回数组指针的方法有问题,我想知道我是否正确地迭代了它们 问:以下是否总是返回正确的数组lenC++ 如何使用长度未知的指针遍历数组?,c++,c,arrays,pointers,multidimensional-array,C++,C,Arrays,Pointers,Multidimensional Array,我已经得到了一个c api和最低限度的文档。 开发人员目前不在,他的代码返回了意外的值(数组的长度不是预期的) 我对返回数组指针的方法有问题,我想知道我是否正确地迭代了它们 问:以下是否总是返回正确的数组len int len=sizeof(sampleState)/sizeof(short); int len=sizeof(samplePosition)/sizeof(int); typedef unsigned char byte; int len=sizeof(volume)/siz
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");
}
所以我用了%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");
}
sizeof(array)/sizeof(element)
技巧仅在您有实际数组而不是指针时有效。如果只有一个指针,就无法知道数组的大小;必须将数组长度传递给函数
或者最好使用向量
,它具有大小
功能
int len=sizeof(sampleState)/sizeof(short);
int len=sizeof(samplePosition)/sizeof(int);
sizeof是在编译时完成的,因此如果在编译时数组的长度未知(例如,使用malloc保留内存),那么这种方法就不起作用
仅当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::vector < /C> >或<代码> STD::数组到手动分配的数组;尽管这对您没有帮助,因为尽管有疑问标记,您在这里使用的是C。好的,忽略我在上面使用的方法,这是完全错误的-尽管您需要知道我最终从API开发人员那里获得的数组长度。您标记了两种不同的编程语言。正确答案会因你所指的语言而异。你想要C++还是C回答?编译时间,不是预编译时间,不一定是编译时-<代码> sieOS/<代码>在C99可变长度数组的运行时完成。我犯了一个错误,“P.EX”并不是什么意思。我应该用例如。
sizeof(sampleState)/sizeof(short);
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
}