为什么这个C程序无法正常运行?

时间:2016-02-22 22:58:18

标签: c arrays memory malloc declaration

这个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

为什么我的输出被破坏了?

1 个答案:

答案 0 :(得分:1)

数组DateTime dateTime = dateTimePicker1.Value; string theDate = dateTime.ToString("ddMM") + dateTime.Year.ToString("D4").Substring(1); 是具有自动存储持续时间的函数的本地数组。它在函数退出后被销毁。所以指向数组元素的所有指针都变得无效。

我可以建议以下解决方案

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