Unable to put strings in an array after initialisation - CCS 6 with TI compiler 5.1.8 for ARM

1.2k views Asked by At

I'm trying to setup a multilanguage GUI for an application running on AM335x processor; developing in CCS 6.0.1 and using TI compiler 5.1.8. The concept is to get enumerated dictionary arrays and then switch current dictionary pointer to one of them, so that Im able to use enums that make sense.

enum dictionary_indexes {
name,
surname,
phone
}

const char *dictionary_en[60];

dictionary_en[name] = "Your name";
dictionary_en[surname] = "Your surname";
//and so on

Unfortunately, CCS wont compile such code. Itll only allow array initialized at the moment of declaration:

//compiles nicely:
const char * const teststring[] = {
     "String1",
     "String2",
};

//doesn't compile:
const char *teststring2[2];
teststring2[0]="Hello";
teststring2[1]="World";

Such code results in an error

a value of type "char [6]" cannot be used to initialize an entity of type "int [0]"

and so for every array entry.

Am I missing something here? I've used such code in the past and worked fine. Is it a compiler issue with TI, or is the issue specific for the processor? The code that is supposed to be working is based on this thread: How do I create an array of strings in C?

2

There are 2 answers

0
Kurak On

The teststring2 has to be a global variable, but its init can't. A little rafactor to enclose the init in an executable funciton brings relief and proper compilation, as suggested by @barakmanos.

const char *teststring2[2];

void initDict(){

    teststring2[0]="Hello";
    teststring2[1]="World";
}
0
Nisse Engström On

A C file, a translation-unit, can only contain two types of elements (after pre-processing): function-definition's and declaration's. A declaration provides the type of an object and an optional initializer. What you have are statement's, which are only allowed inside a function-definition.

In other words, you need to provide the initialization at the point of declaration, or move them inside a function as an ordinary assignment. Eg:

enum dictionary_indexes {
  name,
  surname,
  phone
}

const char *dictionary_en[60] = {
  [name] = "Your name",
  [surname] = "Your surname"
};

or:

void f (void)
{
  dictionary_en[name]    = "Your name";
  dictionary_en[surname] = "Your surname";
}

Note that the { [name] = ..., } syntax in initializers was introduces in C99. If you have a compiler that conforms to an earlier standard, you need to initialize the array without designator's and in the correct order:

const char *dictionary_en[60] = {
  "Your name",
  "Your surname"
};