Knowing how many elements I have in an Arduino array

0

I have an ARRAY of at most 100 lines by 2 columns, which will be filled by a user.

My program will play back user activity later.

What I want to know is how to know how many rows were filled with data. Is there any C command for this?

    
asked by anonymous 07.05.2016 / 23:59

1 answer

0

Ideally, you should start by cleaning up any type of value inside the array.

int x; // limpa os valores do array
for(x=0;x<100;x++){
    array[x][0] = 0;
    array[x][1] = 0;
}

Then the method for insertion is done.

void array_insert(int *data, int **array){
    if(array[99][0] && array[99][1])
        return; //encerra caso o array esteja cheio

    int pos = 0;
    while(array[pos][0] && array[pos][1]) // procura uma posição vazia
        pos++;

    array[pos][0] = data[0]; // insere valor na posição livre do array
    array[pos][1] = data[1];
}

And the count:

int array_len(int **array){
    int x=0;
    while(array[x][0] && array[x][1])
        x++;

    return x;
}
  

Note: Since you said that you were working with a 100 * 2 array , the values are double pointers so you can access the positions without error.

    
08.05.2016 / 00:23