这个C程序将字符串“1 2 3 4 5 6 7 8 9 10”分割成标记,存储在buf
中,并打印buf
的内容。
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/* Fills an array with pointers to the tokens of the given string.
* string: A null-terminated char*
* buf: A buffer to be filled with pointers to each token.
*/
void get_tokens(char * string, char ** buf) {
char tmp[100];
strncpy(tmp, string, 100);
char * tok = strtok(tmp, " \n");
int i = 0;
while (tok != NULL) {
buf[i] = tok;
tok = strtok(NULL, " \n");
i++;
}
}
int main() {
char ** buf = malloc(10 * sizeof(char*));
char * string = "1 2 3 4 5 6 7 8 9 10";
get_tokens(string, buf);
int i;
for (i = 0; i < 10; i++) {
printf(" %s\n", buf[i]);
}
}
输出:
1
2
3
4
s�c8
�c8
8
9
10
为什么我的输出被损坏了?
最佳答案
数组tmp
是具有自动存储持续时间的函数的本地数组。它在函数退出后被销毁。所以所有指向数组元素的指针都将失效。
我可以建议以下解决方案
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
size_t get_tokens( char ** buf, size_t n, const char *string )
{
char tmp[100];
strncpy( tmp, string, 100 );
tmp[99] = '\0';
size_t i = 0;
char * tok = strtok( tmp, " " );
while ( i < n && tok != NULL )
{
buf[i] = malloc( strlen( tok ) + 1 );
strcpy( buf[i++], tok );
tok = strtok( NULL, " " );
}
return i;
}
int main( void )
{
const size_t N = 10;
char ** buf = malloc( N * sizeof( char * ) );
char * string = "1 2 3 4 5 6 7 8 9 10";
size_t n = get_tokens( buf, N, string );
for ( size_t i = 0; i < n; i++ )
{
puts( buf[i] );
}
for ( size_t i = 0; i < n; i++ ) free( buf[i] );
free( buf );
return 0;
}
程序输出是
1
2
3
4
5
6
7
8
9
10
至于您的程序,那么至少应该用存储说明符
static
声明数组。例如
static char tmp[100];
^^^^^^
strncpy( tmp, string, 100 );
tmp[99] = '\0';
^^^^^^^^^^^^^^^
关于c - 为什么此C程序无法正常运行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35565613/