二维字符数组存放数据和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停止打印了呗。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值