I’ve been given a c api to work with and the minimum docs.
Developer is not around at the moment and his code is returning unexpected values (arrays not of expected length)
Im having problems with methods that return pointers to arrays and was wondering am I iterating over them correctly.
Q:does the following always return the correct len of an array?
int len=sizeof(sampleState)/sizeof(short);
int len=sizeof(samplePosition)/sizeof(int);
typedef unsigned char byte;
int len=sizeof(volume)/sizeof(byte);
And I iterate over the array using the pointer and pointer arithmetic (am I doing it correctly for all types below)
And last example below is multidimensional array? Whats the best way to iterate over this?
thanks
//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");
}
Here byte is type def to
typedef unsigned char byte;
so I used %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");
}
Here is int[][] soundFXStatus.
do I just use same method above and have 2 loops?
//--------------
int** soundFXStatus = mixerState->soundFXStatus;
The
sizeof(array)/sizeof(element)trick only works if you have an actual array, not a pointer. There’s no way to know the size of an array if all you have is a pointer; you must pass an array length into a function.Or better use a
vector, which has asizefunction.