Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over a array using a pointer and length unknown?

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;
like image 989
brian.clear Avatar asked Apr 02 '12 15:04

brian.clear


People also ask

How do you traverse an unknown length array in C++?

So if as you say the array has unknown size then the compiler can not calculate the size of the memory occupied by the array. So it is evident that anArray is pointer to first element of the array that you might pass to the function. sizeof( double * )/sizeof( double ).

How do you find the length of an array using pointers?

Using the sizeof() operator and using pointer arithmetic. The sizeof() operator in C calculates the size of passed variables or datatype in bytes. Therefore to find the array's length, divide the total array size by the size of one datatype that you are using.

Which of them is the easy way to iterate over arrays?

You can iterate over an array using for loop or forEach loop. Using the for loop − Instead on printing element by element, you can iterate the index using for loop starting from 0 to length of the array (ArrayName. length) and access elements at each index.


2 Answers

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 a size function.

like image 59
Mark Ransom Avatar answered Oct 19 '22 18:10

Mark Ransom


sizeof(sampleState)/sizeof(short);

This will only give the length of an array if sampleState is declared as an array, not a pointer:

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

Also, beware that a function argument is actually pointer even if it looks like an array:

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

In your code, sampleState is a pointer; there is no way to determine the length of an array given only a pointer to it. Presumably the API provides some way to get the length (since otherwise it would be unusable), and you'll need to use that.

In C++, this is one reason why you would prefer std::vector or std::array to a manually allocated array; although that doesn't help you since, despite the question tags, you are using C here.

like image 40
Mike Seymour Avatar answered Oct 19 '22 17:10

Mike Seymour