How to count how many elements an array has?

3

I have a array of 10 positions that is filled by a function,

ARMultiEachMarkerInfoT marcadoresVisiveis[10];

Always before adding elements to array , the function clears all elements and populates again. I want to know the best way to count how many elements array has, I tried something like: count how many non-null elements the array has and increment a counter but I did not succeed.

Follow 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...
}
    
asked by anonymous 27.04.2017 / 15:27

1 answer

3

It would look something like this:

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 placed it on GitHub for future reference .

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

    
27.04.2017 / 16:00