C中二维字符数组的行数
Amount of lines of a 2D Char array in C
是否可以计算二维 char
数组的线?
上下文:
我已经从 csv 文件中读取了名称,我想用它后面的数字进行计算。
伪代码:
int workers = size of the 2d char array (a_name)
workers = sizeof(a_name)
显然是不行的。
1.你可以在读取csv文件的同时统计worker的数量
2. 你可以写一个函数来计算工人的数量(取决于你期望的工人数量,这可能是值得的)。
无论如何,如果你要使用 strlen - 它会 运行 遍历整个东西,直到它到达第一个 '\0'。
它应该看起来像这样
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
另一种可能的解决方案(不确定我这样做是否正确)
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
是否可以计算二维 char
数组的线?
上下文:
我已经从 csv 文件中读取了名称,我想用它后面的数字进行计算。
伪代码:
int workers = size of the 2d char array (a_name)
workers = sizeof(a_name)
显然是不行的。
1.你可以在读取csv文件的同时统计worker的数量
2. 你可以写一个函数来计算工人的数量(取决于你期望的工人数量,这可能是值得的)。 无论如何,如果你要使用 strlen - 它会 运行 遍历整个东西,直到它到达第一个 '\0'。
它应该看起来像这样
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
另一种可能的解决方案(不确定我这样做是否正确)
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