tk421x
tk421x

Reputation: 57

error: default initialization of an object of const type 'const char'

I am getting this error -- error: default initialization of an object of const type 'const char'

my code is in c and the error is in my .h file.. this is what the code looks like that is getting the errors

const char *c2ptr[37]={ 0, 4, 8,11,14,16,18,20,23,25,
           27,29, 3,31,34,37, 3, 3,39,44,
           48,51,54,57,60, 3,62,65, 3,68,
           72,75, 3,77,79,81,84},
    ch,inif,*fname,
    comf[4]={ 7, 8, 9,13};

and so... the errors all pop up on the ch,inif,*fname... does it have to do with the fact that they are not arrays? as far as i know this is legal.

Upvotes: 2

Views: 8194

Answers (2)

Freyja
Freyja

Reputation: 40934

Your code declares four variables:

  • An array c2ptr of items of type const char*
  • A constant ch of type char
  • A constant inif of type char
  • A variable fname of type const char*
  • A constant array comf of items of type char

First of all, c2ptr is an array of const char* (strings), but your code initializes it to a list of int, { 0, 4, 8, 11, ... }.

Secondly, your variables ch and inif are constants, but haven't been assigned a value to them. Since a constant variable can't be changed, you must assign a value to them in the declaration. (This is probably what's giving you errors.)

Upvotes: 4

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53036

You declared the array for const char * pointers, and initialized it with int, maybe you meant

static const char *c2ptr[37] = {
        "0",  "4",  "8", "11", "14", "16", "18", "20", "23", "25",
       "27", "29",  "3", "31", "34", "37",  "3",  "3", "39", "44",
       "48", "51", "54", "57", "60",  "3", "62", "65",  "3", "68",
       "72", "75",  "3", "77", "79", "81", "84"
   };

Upvotes: 1

Related Questions