C--二次元アレイ、ポインタの配列、ポインタ配列(文字列単純な例)

プログラムコード

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

/*
    二维数组表示方法如下
    1.数组名 
    2.指针数组
    3.数组指针(只能用来存储二维数组的数组名[即二维数组的首地址],不可用来存储内容)
*/

// 二维数组
// ① char testStr1[10][6] = {"begin", "ifgbd", "while", "dosfa", "abend"}; // 字符串的存储方式 1.字符数组 2.字符指针变量

char testStr1[10][5] = {{'b','e','g','i','n'}, {'i','f','g','b','d'}, {'w','h','i','l','e'}, {'d','o','s','f','a'}, {'a','b','e','n','d'}}; 

/*
    注意:

    char str1[10] = {'s','t','u','d','e','n','t'};
    在内存中的存放形式为:
    s t u d e n t 【6】 
    
    
    char str2[10] = "student";  // 在存储字符串时末尾自动加上字符串的结束标志'\0'
    在内存中的存放形式为:
    s t u d e n t \0 【7】这是为什么前面 testStr1[10][6] 要这样定义,因为每个字符串的总长度为5,但是"begin"以这样的
    形式就会在内存中自动多存储一个结束标志'\0',所以要多定义一个空间
    
    "student" 无论是单独成立的,还是放在大括号里,其存储在内存中都是会自动加上字符串的结束标志'\0'
*/


// 指针数组
char *testStrs[5] = {"begin", "ifgbd", "while", "dosfa", "abend"};

// 数组指针
// 与前面的① 相对应char (*testStr)[6];
char (*testStr)[5]; // 注意这里不能像前面两种形式那样赋值

int main(){
    int i = 0;
    int j = 0;

    for(i = 0; i < 5; i++){
        for(j = 0; j < 5; j++){
            printf("%c",*(*(testStrs + i) + j)); // printf("%c",*(testStrs[i] + j));   testStrs[i] <==> *(testStrs + i)
        }
        
        printf("\n");
    }


    printf("\n");
    
    testStr = testStr1;
    for(i = 0; i < 5; i++){
        for(j = 0; j < 5; j++){
            printf("%c",*(*(testStr + i) + j));
        }
        printf("\n");

    }

    return 0;
}

業績

おすすめ

転載: www.cnblogs.com/zwxo1/p/11846036.html
おすすめ