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

时间:2021-12-11 21:30:16

I have two Character arrays, I would like to make one 2 Dimensional array. but the Character values seem to be causing a problem, in the way that I tried to initialize them in the 2D array.

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

what is the proper way to initialize this type of array? The function "trumplar()" works fine, or as I would expect.

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

The 2D character array x[22][22] function "trumpsterFire()" fails to be initialized properly.

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 个解决方案

#1


2  

Use single qoute (') to assign a character.

使用单个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

    ;
        }

#1


2  

Use single qoute (') to assign a character.

使用单个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

    ;
        }