Search code examples
cpointerscatch2

Assigning a char* to a char


So I'm very new to C, and I'm just starting to use pointers. I'm using a 2D array to convert a set of strings from hexadecimal to decimal, letter by letter. However, in the process, I have to take the first character of each string, and to do that I'm trying to assign them to a char. This line specifically is giving me trouble.

aChar = input[j][i];

It keeps saying I can't convert from const char* to char, but no matter how I change it, I can't seem to get it to work.

Heres the full program:

#include <stdio.h>
#include "catch.hpp"
#include <unistd.h> 
TEST_CASE("Listing 2.2")
{
    int x; 
    int j = 0;
    int i = 0;
    x = 0; 
    const int N = 8;
    char aChar;
    const char* input[N][5] = {"a000", "ffff", "0400", "1111", "8888", "0190", "abcd", "5555"};
    int answers[N] = {40960, 65535, 1024, 4369, 34952, 400, 43981, 21845};
    for (j=0; j>N; j++){
        for(i=0; i>5; i++){
            aChar = input[j][i];
            x = x << 4;                  
            if (aChar <= '9') 
            { 
                x = x + (int)(aChar & 0x0f); 
            } 
            else 
            { 
                aChar = aChar & 0x0f; 
                aChar = aChar + 9; 
                x = x + (int)aChar; 
            }
            CHECK(answers[j] == x); 
        }//end for 1
    }//end for 2 
    printf("End of program.\n"); 
}

Any help would be appreciated!


Solution

  • input is a 2D array of pointer to characters. Effectively it has 3 dimensions. Two Dimensions of strings of N*5 and the third dimension will traverse the characters in each string.

    const char* input[N][5]
    

    If you want the first character of each string, you need to use

    aChar = input[j][i][0];