Char type array

S

Sava Mikalaèki

Hi there!

My problem consists in this:
When I define an array of char type like this:
char a[]={'a','aa','aaa'};
I get a lot of warnings like
warning C4305: 'initializing' : truncation from 'const int' to 'char'
I've tryed to define the size of it (char a[10] ) but I get same warnings
every time.
What's wrong with this?
Poz,
Sava
 
R

Ron Natalie

Sava Mikalaèki said:
Hi there!

My problem consists in this:
When I define an array of char type like this:
char a[]={'a','aa','aaa'};
I get a lot of warnings like
warning C4305: 'initializing' : truncation from 'const int' to 'char'
I've tryed to define the size of it (char a[10] ) but I get same warnings
every time.
What's wrong with this?

char is a single charater. 'a' is a single character.
The constant 'aa' is an implementation defined int consant.
It is NOT a string literal. A string literal would be "".

char a[] is an array of (with this initialization) 3 characters.
It is not an array of strings.

If you want strings, do this:

#include <string>
std::string a[] = { "a", "aa", "aaa" };
 
A

Andrey Tarasevich

Sava said:
...
My problem consists in this:
When I define an array of char type like this:
char a[]={'a','aa','aaa'};
I get a lot of warnings like
warning C4305: 'initializing' : truncation from 'const int' to 'char'
I've tryed to define the size of it (char a[10] ) but I get same warnings
every time.
What's wrong with this?
...

In C/C++ 'aa' and 'aaa' are integer constants. The compiler already
explained the rest. You are trying to initialize members of 'char' array
with these constants, which results in truncation.

There's no way to say what else is wrong with this until you explain
what is that you are trying to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top