简体   繁体   中英

How can I initialize this 2D array, in C?

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.

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

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

#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;
    }

Use single qoute (') to assign a character.

    #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

    ;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM