How many elements does an array have?

Asked

Viewed 607 times

3

I have a array of 10 positions which is filled through a function,

ARMultiEachMarkerInfoT marcadoresVisiveis[10];

always before adding elements to the array, the function clears all elements and fills again. I want to know how best to count how many elements the array has, tried something like: count how many non-null elements the array has and increments a counter but was unsuccessful.

Follows the function:

void ReposicionaObjetos(){
    int i;

    //Limpa o array
    memset(marcadoresVisiveis, 0, sizeof marcadoresVisiveis);

    //Preenche o array
    for( i = 0; i < config->marker_num; i++ ) {
        if( config->marker[i].visible >= 0 ){
            marcadoresVisiveis[i] = config->marker[i];
        }
    }

    //... Saber quantos elementos o array possui...
}

1 answer

3


It would be something like that:

void ReposicionaObjetos(){
    int contaElementosValidos = 0;
    memset(marcadoresVisiveis, 0, sizeof marcadoresVisiveis);
    for int (i = 0; i < config->marker_num; i++) {
        if (config->marker[i].visible >= 0 ){
            marcadoresVisiveis[i] = config->marker[i];
            contaElementosValidos++;
        }
    }
    //faz o que precisa com contaElementosValidos
}

I put in the Github for future reference.

I have serious doubts whether this approach to clean up array and popular again is the correct, but as I did not give the context I can not talk much.

  • I share the doubts about clearing the array, but for now, it’s enough. Simple solution, sometimes escapes the eyes and we try to look for something more complex or simply do not know how to look, thanks, it worked exactly as expected.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.