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); 
                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!

5 answers

  • answered 2018-09-21 06:28 Rishikesh Raje

    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];

  • answered 2018-09-21 06:41 Serge Ballesta

    Here is the culprit:

    const char* input[N][5] = {"a000", "ffff", "0400", "1111", "8888", "0190", "abcd", "5555"};

    You want to declare an array to 8 strings of size 5 (4 digits + terminating null) but actually declare an array of 8 arrays of 5 pointers.

    What you want is:

    const char input[N][5] = {"a000", "ffff", "0400", "1111", "8888", "0190", "abcd", "5555"};

  • answered 2018-09-21 06:53 CIsForCookies

    I'm using a 2D array

    In order to use a 2D array of char you should declare: const char input[N][5]. const char* input[N][5] is a 2D array of char* (instead of an array of strings you get a 2D array of strings).

    Given your declaration, no wonder that a aChar = input[j][i]; results in an assignment of a char* to a char, which is an error.

  • answered 2018-09-21 07:27 Matthias

    You used the wrong condition for your for loops. You initialized your loop variable (i/j) with 0 but you ask for the loops to be terminated if the i/jis bigger than 5/N. If you want to iterate over your whole 2 dimensional array it should look like this: ( '<' instead of '>')

    for (j=0; j<N; j++){
        for(i=0; i<5; i++){
        //... your code
        }//end for 1
    }//end for 2 

  • answered 2018-09-21 08:17 sakthi

    aChar = input[j][i]; In this statement, you are trying to assigning const pointer address to char, It is wrong statement.

    You need to reference the pointer variable then assign to the corresponding variable, Can you change the statement as, aChar = *input[j][i];