如何在C中初始化这个2D数组?

时间:2016-11-16 01:56:22

标签: c arrays

我有两个Character数组,我想制作一个2维数组。 但是,我试图在2D数组中初始化它们时,Character值似乎引起了问题。

初始化这种类型数组的正确方法是什么? 功能“trumplar()”工作正常,或正如我所料。

2D字符数组x [22] [22]函数“trumpsterFire()”无法正确初始化。

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

void trumplar(){
int len = 22;
         char a[25]={0x3f,0x6,0x5b,0x4f,0x66,0x6d,0x7d,0x7,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x3d,0x76,0x1e,0x38,0x38,0x6d,0x00};
         char L[25]="0123456789abcdefghjlpsS";
    int i;
    for (i = 0; i <=len; i++){
        char hit=L[i];
        char urd=a[i];
        printf("The %d, Value of a is:%c\t Hex val: %c\n",i,hit,urd);
        }  
}  

void trumptsterFire(){
    //int xlen = 22;
char x[22][22]={
    {0x3f,0},{0x6,1},{0x5b,2},
    {0x4f,3},{0x66,4},{0x6d,5},
    {0x7d,6},{0x7,7},{0x7f,8},
    {0x6f,9},{0x77,a},{0x7c,b},
    {0x39,c},{0x5e,d},{0x79,e},
    {0x71,f},{0x3d,g},{0x76,h}
    ,{0x1e,j},{0x38,l},{0x38,p},
    {0x6d,s},{0x00,S}
    };  
    }       

int main(){
    trumplar();
trumptsterFire();
    return 0;
    }

1 个答案:

答案 0 :(得分:2)

使用单个qoute(')分配角色。

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

        void trumplar(){
        int len = 22;
                 char a[25]={0x3f,0x6,0x5b,0x4f,0x66,0x6d,0x7d,0x7,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x3d,0x76,0x1e,0x38,0x38,0x6d,0x00};
                 char L[25]="0123456789abcdefghjlpsS";
            int i;
            for (i = 0; i <=len; i++){
                char hit=L[i];
                char urd=a[i];
                printf("The %d, Value of a is:%c\t Hex val: %c\n",i,hit,urd);
                }  
        }  

        void trumptsterFire(){
            //int xlen = 22;
        char x[22][22]={
            {0x3f,'0'},{0x6,'1'},{0x5b,'2'},
            {0x4f,'3'},{0x66,'4'},{0x6d,'5'},
            {0x7d,'6'},{0x7,'7'},{0x7f,'8'},
            {0x6f,'9'},{0x77,'a'},{0x7c,'b'},
            {0x39,'c'},{0x5e,'d'},{0x79,'e'},
            {0x71,'f'},{0x3d,'g'},{0x76,'h'}
            ,{0x1e,'j'},{0x38,'l'},{0x38,'p'},
            {0x6d,'s'},{0x00,'S'}
            };  
            }       

        int main(){
            trumplar();
        trumptsterFire();
            return 0

    ;
        }