C中的双阵列打印输出给了我奇怪的输出

时间:2014-03-16 00:07:55

标签: c

     a = (float**)malloc(num * sizeof(float*));
     if( !a)  {
     printf("Cannot allocate a!\n");
     exit(1);
     }

     for(i = 0; i < num; i++) {
     a[i] = (float *)malloc(num * sizeof(float));
     if( !a[i]) {
            printf("Cannot allocate a[%d]!\n",i);
            exit(1);
     }}

    for(i = 0; i < num; i++) {
        for(j = 0; j < num; j++)
            fscanf(fp,"%f ",&a[i][j]);
    }


    // in a method called after a is initialized and elements are recorded. 
    int s, t;
    printf("This is num: %d\n", num);
    for (s = 0; s < num; s++) {

            for (t = 0; t < num; t++) {
                    printf("This is a[s][t]:%d for s:%d and t:%d\n", a[s][t], s, t);
            }
    }
  

这是数字:3   对于s:0和t:4198765,这是[s] [t]:0   对于s:1和t:4199061,这是[s] [t]:0   对于s:2和t:4199061,这是[s] [t]:0   对于s:0和t:4199061,这是[s] [t]:1   对于s:1和t:4199061,这是[s] [t]:1   对于s:2和t:4199061,这是[s] [t]:1   对于s:0和t:4199061,这是[s] [t]:2   对于s:1和t:4199061,这是[s] [t]:2   对于s:2和t:4199061

,这是[s] [t]:2

//这是矩阵输入 5 1 3 6 3 7 2 8 3 6 9 6

//订购甚至不对!

1 个答案:

答案 0 :(得分:0)

您应该使用%f打印浮动广告,并检查每个fscanf是否已成功或失败。