0

I have a global variable declared in .h file:

extern char Title[10];

when I use it in the .cpp file:

char Title[10] = "Asia";

more code:

Title[10] = "Europe";

this second assignment is causing an error error: invalid conversion from 'const char*' to 'char' global variable

How can I reassign this global variable?

PeterSW
  • 4,921
  • 1
  • 24
  • 35
user2333234
  • 553
  • 1
  • 4
  • 7
  • 1
    Title[10] is a char and "Europe" is a string(i.e. const char*). They are not equivalent. use strcpy function to copy c style strings. – jodag Apr 10 '14 at 23:05
  • 5
    Also, this has nothing to do with the variable being global. – jodag Apr 10 '14 at 23:07
  • @jodag: A string is a data format (0-terminated array of `char`), not a data type (it is accessed using `char*`). A string literal is the `const`-qualified version found directly in source code. – Deduplicator Apr 10 '14 at 23:23
  • @Deduplicator You're right. – jodag Apr 10 '14 at 23:34
  • Is this really a C++ question? The only thing C++ like seems to be the naming of the source file. – PeterSW Apr 10 '14 at 23:37

4 Answers4

2

The problem:

extern char Title[10];

Declares a char array of size 10.

While:

Title[10] = "Europe";

attempts to set the 11th element of Title to "Europe".

Obviously not what you were intending...

The C style solution:

Use strcpy to copy the char array "Europe" into Title. Here's some handy reference to strcpy.

You pass it the destination and source so in your case it would be:

strcpy(Title, "Europe");

The C++ route:

Use a string class such as std::string. Here's some handy reference to std::string

extern std::string Title;

...

std::string Title("Asia");
Title = "Europe";
PeterSW
  • 4,921
  • 1
  • 24
  • 35
0

In the char Title[10] = "Asia" line, you are initializing the array to those values. In the line where you set it to Europe, you are trying to set the tenth byte of the character array to a pointer to a constant string. You should use strncpy or some such technique to copy into the string.

e.g. strncpy(Title, "Europe", 10);

nortoon
  • 566
  • 4
  • 8
0

This is a declaration; it declares an array named Title that is of type char and contains 10 elements:

char Title[10] = "Asia";

This, however, is an assignment statement. It attempts (incorrectly) to assign a string to the 11th element of Title:

Title[10] = "Europe";

You cannot assign a string to a char, which is what Title[10] is. What you want to do is copy a new string to Title, and the easiest way to do that is with the strcpy function. Like this:

strcpy(Title, "Europe");
Carey Gregory
  • 6,836
  • 2
  • 26
  • 47
0
error: invalid conversion from 'const char*' to 'char' global variable

is caused by trying to assign a string to a single char. Besides this you invoke undefined behavior before you get to this.

extern char Title[10]; // array of 10 char, indexed as 0, 1, ..., 9

This is declaration, OK.

char Title[10] = "Asia";

This is initialization, OK.

Title[10] = "Europe";  // 10 means accessing array out of bound

Access array out of bounds. This is undefined behavior.


C++ Standard n3337 § 5.2.1/1

A postfix expression followed by an expression in square brackets is a postfix expression. One of the expres- sions shall have the type “pointer to T” and the other shall have unscoped enumeration or integral type. The result is an lvalue of type “T.” The type “T” shall be a completely-defined object type.62 The expression E1[E2] is identical (by definition) to *((E1)+(E2)) [ Note: see 5.3 and 5.7 for details of * and + and 8.3.4 for details of arrays. — end note ]

Then, § 5.7/5 explains what ptr + i points to:

(...)If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.

4pie0
  • 29,204
  • 9
  • 82
  • 118