简体   繁体   中英

Amount of lines of a 2D Char array in C

Is there a posibility to calculate the lines of a 2d char array?

Context:

I've read names out of a csv file and i want to calculate with the numbers behind it.

Pseudocode:

int workers = size of the 2d char array (a_name)

workers = sizeof(a_name) is obviously not working.

1. you can count the number of workers while reading the csv file.

2. you can let write a function that will count the number of workers ( depends on the number of worker you're expecting it might be worth it ). anyway if you're gonna use strlen - it's gonna run over the entire thing till it reaches the first '\\0'.

it should look something like this

char workers[ MAX_WORKERS ][ MAX_NAME_LENGTH ];
int counter;

// assuming the array was filled from 0 sequntially
for ( counter = 0; counter < MAX_WORKERS; counter++ ) {
    if ( *(workers[ i ]) == 0 )
         break;
}

// counter now holds the number of workers

another possible solution ( not sure if I'm doing this right )

char workers[ MAX_WORKERS ][ MAX_NAME_LENGTH ];
char *pointer;
int counter

counter = 0;
pointer = workers;

// assuming a memset or calloc have been made for workers, and that the array was filled from 0 sequntially
while( pointer != NULL ) {
    pointer += MAX_NAME_LENGTH;
    counter++;
}

// counter now holds the number of workers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM