0

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;
    }
2
  • Are you adding that last \0 element to the arrays? Commented Nov 16, 2016 at 2:02
  • 1
    Your array dimensions don't make much sense. You've got char x[22][22] (array of 22 arrays of 22 chars) but then an initialiser list that would match char x[23][2] (array of 23 arrays of 2 chars)... (ignoring the missing single quotes) Commented Nov 16, 2016 at 2:15

1 Answer 1

2

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

    ;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

the \0 character isnt required, Im just ignoring it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.