二维字符数组存放数据和printf遇见NULL

存放数据:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
//3x3字符数组,每个位置只能存放一个字符
//{'11','12','13'},这样会存放后面的字符,啊哈哈

        char er[3][3]={
                {'1','2','3'},
                {'4','5','6'},
                {'7','8','9'}
        };
        //存放
        strcpy(er[1],"abcdef");
        for(int i=0;i<3;i++){
                for(int j=0;j<3;j++){
                        printf("the [%d][%d] is %d\n",i,j,er[i][j]);
                }
        }
        return 0;
}
the [0][0] is 49
the [0][1] is 50
the [0][2] is 51
the [1][0] is 97
the [1][1] is 98
the [1][2] is 99
the [2][0] is 100
the [2][1] is 101
the [2][2] is 102

打印:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
        char er[3][4]={
                {'1','2','3'},
                {'4','5','6'},
                {'7','8','9'}
        };
        printf("the first row is %s\n",er[0]);
        return 0;
}
the first row is 123

如果er是3x3的,数据填入不变,结果就是这样:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
        char er[3][3]={
                {'1','2','3'},
                {'4','5','6'},
                {'7','8','9'}
        };
        printf("the first row is %s\n",er[0]);
        printf("the second row is %s\n",er[1]);
        return 0;
}
the first row is 123456789@
the second row is 456789@

咋回事呢?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
        char er[3][3]={
                {'1','2'},
                {'4','5',NULL},
                {'7','8','9'}
        };
        printf("the first row is %s\n",er[0]);
        printf("the third num is %s\n",er[0][2]);
        printf("the second row is %s\n",er[1]);
        return 0;
}
the first row is 12
the third num is (null)
the second row is 45

就是printf遇见NULL停止打印了呗。

猜你喜欢

转载自blog.csdn.net/u010931295/article/details/90603404