Home > Software engineering >  Strange symbols while using 2d array printf
Strange symbols while using 2d array printf

Time:10-27

I have a task to make a mini-T9. Something like it was in old phones but user shouldn't click multiple times to get a letter. For example, if user prints 2, it can be A, B or C, if he prints 23, it is a combination of {'a', 'b', 'c'} and {'d', 'e', 'f'}. I also have a file with list of names. Program should return all names which has combination of {'a', 'b', 'c'} and {'d', 'e', 'f'}.

Talking about the problem: I tried to make a 2D array that consist of elements with letters I need. I also wrote a part of code that should check if everything is OK and here I do have a problem. The result of running the program is random symbols or just emptiness. Also, number of elements of array is wrong as it shown in the screenshot.

I have such code:

int main (int argc, char **argv)
{
    int size = strlen(argv);
    char letters[size][4];

    for (int i = 0; argv[1][i] != '\0'; i  )
    {
        switch(argv[1][i])
        {
            case '2':
                letters[i][4] = 'a', 'b', 'c', '\0';
            case '3':
                letter[i][4] = 'd', 'e'. 'f', '\0';
            case '4':
                letter[i][4] = 'g', 'h'. 'i', '\0';
            case '5':
                letter[i][4] = 'j', 'k'. 'l', '\0';
            case '6':
                letter[i][4] = 'm', 'n'. 'o', '\0';
            case '7':
                letter[i][4] = 'p', 'q'. 'r', 's';
            case '8':
                letter[i][4] = 't', 'u'. 'v', '\0';
            case '9':
                letters[i][4] = 'w', 'x', 'y', 'z';
        }
    }

    // the next code was written to check if everything is OK

    for (int e = 1; e <= sizeof(letters)/sizeof(letters[0]); e  )
    {
        for (int j = 0; j <= sizeof(letters[0])/sizeof(letters[0][0]); j  )
        {
             printf("Letters[%d][%d]: %c\n", e, j, letters[e][j]);
        }
    }
}

I'd be extremely grateful for your help.

CodePudding user response:

Here are the issues that I fixed:

  1. Added missing includes.
  2. Check that argv[1] is set, otherwise display a usage.
  3. strlen(argv[1]) instead of strlen(argv) which makes no sense.
  4. Use strcpy() instead of the invalid syntax.
  5. Initialize lettres[i] in the loop as you print data past the initial trailing '\0'.
  6. Added break in each case.
  7. Fixed range of e which was off-by-one
  8. Fixed range for j it should be < not <=.
#include <stdio.h>
#include <string.h>

int main (int argc, char **argv) {
    if(argc != 2) {
        printf("usage: %s integer\n", *argv);
        return 0;
    }
    int size = strlen(argv[1]);
    char letters[size][5];
    for (int i = 0; argv[1][i]; i  ) {
        memset(letters[i], 0, sizeof(*letters) / sizeof(**letters));
        switch(argv[1][i]) {
            case '2':
                strcpy(letters[i], "abc");
                break;
            case '3':
                strcpy(letters[i], "def");
                break;
            case '4':
                strcpy(letters[i], "def");
                break;
            case '5':
                strcpy(letters[i], "jkl");
                break;
            case '6':
                strcpy(letters[i], "mno");
                break;
            case '7':
                strcpy(letters[i], "pqrs");
                break;
            case '8':
                strcpy(letters[i], "tuv");
                break;
            case '9':
                strcpy(letters[i], "wxyz");
                break;
        }
    }

    for (int e = 0; e < sizeof(letters)/sizeof(letters[0]); e  ) {
        for (int j = 0; j < sizeof(letters[0])/sizeof(letters[0][0]); j  ) {
            printf("Letters[%d][%d]: %c\n", e, j, letters[e][j]);
        }
    }
}

and here is the output with 572:

Letters[0][0]: j
Letters[0][1]: k
Letters[0][2]: l
Letters[0][3]: 
Letters[0][4]: 
Letters[1][0]: p
Letters[1][1]: q
Letters[1][2]: r
Letters[1][3]: s
Letters[1][4]: 
Letters[2][0]: a
Letters[2][1]: b
Letters[2][2]: c
Letters[2][3]: 
Letters[2][4]: 
  • Related