Initialization of const objects.

Z

zouyongbin

The code segment like below will fail at compilation:

int t = 5;
const int ix = t;
char cx[ix] = "hell";

while that like below is correct:

int t = 5;
const int ix = 5;
char cx[iy] = "hell";

Can anybody give me a explanation about what is really happening here?

Thanks.
 
K

Kavya

zouyongbin said:
The code segment like below will fail at compilation:

int t = 5;
const int ix = t;
char cx[ix] = "hell";

while that like below is correct:

int t = 5;
const int ix = 5;
char cx[iy] = "hell";

Can anybody give me a explanation about what is really happening here?

I am not sure but I think since t is not a compile time constant that
is why you are getting error in first code whereas 5 is a compile time
constant due to which you are not getting error in 2nd case.
 
S

Salt_Peter

zouyongbin said:
The code segment like below will fail at compilation:

int t = 5;
const int ix = t;
char cx[ix] = "hell";

This works fine, although rather strange.
char cx[] = "hell";
makes sense, and you can get its size with:
size_t size = sizeof(cx)/sizeof(char);
while that like below is correct:

int t = 5;
const int ix = 5;
char cx[iy] = "hell";

the above does not compile, iy is not declared nor defined.
Can anybody give me a explanation about what is really happening here?

If your asking about the const int, there is no problem there:
const int ix(5); // will do

try:

#include <iostream>

int main()
{
char* p_s = "hell"; // pointer to a literal
std::cout << p_s << std::endl;
}
 
Z

zouyongbin

Salt_Peter said:
zouyongbin said:
The code segment like below will fail at compilation:

int t = 5;
const int ix = t;
char cx[ix] = "hell";

This works fine, although rather strange.
char cx[] = "hell";
makes sense, and you can get its size with:
size_t size = sizeof(cx)/sizeof(char);

My compiler is gcc4.1.2, and I got a compile error here.
while that like below is correct:

int t = 5;
const int ix = 5;
char cx[iy] = "hell";

the above does not compile, iy is not declared nor defined.

Sorry, it's a type error. The "iy" should be "ix" here.

What I am confused here is that whether "ix" is a constant seems to be
influenced by what is assigned to "ix". If "ix" is assigned by a
constant, like 5, it act like a constant. But if "ix" is assigned by a
non-constant such as "t" here, it can not be set as the dimension of an
array.
I think that is weird. Is there any trick here?

Thanks.
 
I

Ian Collins

zouyongbin said:
Salt_Peter said:
zouyongbin said:
The code segment like below will fail at compilation:

int t = 5;
const int ix = t;
char cx[ix] = "hell";

This works fine, although rather strange.
char cx[] = "hell";
makes sense, and you can get its size with:
size_t size = sizeof(cx)/sizeof(char);


My compiler is gcc4.1.2, and I got a compile error here.

while that like below is correct:

int t = 5;
const int ix = 5;
char cx[iy] = "hell";

the above does not compile, iy is not declared nor defined.


Sorry, it's a type error. The "iy" should be "ix" here.

What I am confused here is that whether "ix" is a constant seems to be
influenced by what is assigned to "ix". If "ix" is assigned by a
constant, like 5, it act like a constant. But if "ix" is assigned by a
non-constant such as "t" here, it can not be set as the dimension of an
array.

That's because it isn't a compile time constant.
 
S

Salt_Peter

zouyongbin said:
Salt_Peter said:
zouyongbin said:
The code segment like below will fail at compilation:

int t = 5;
const int ix = t;
char cx[ix] = "hell";

This works fine, although rather strange.
char cx[] = "hell";
makes sense, and you can get its size with:
size_t size = sizeof(cx)/sizeof(char);

My compiler is gcc4.1.2, and I got a compile error here.
while that like below is correct:

int t = 5;
const int ix = 5;
char cx[iy] = "hell";

the above does not compile, iy is not declared nor defined.

Sorry, it's a type error. The "iy" should be "ix" here.

What I am confused here is that whether "ix" is a constant seems to be
influenced by what is assigned to "ix". If "ix" is assigned by a
constant, like 5, it act like a constant. But if "ix" is assigned by a
non-constant such as "t" here, it can not be set as the dimension of an
array.
I think that is weird. Is there any trick here?

Thanks.

Thats the compile time constant issue, fix:

int main()
{
int t(5);
const int ix(t);
char* p_cx = new char[ix];
delete [] p_cx;
}

or better yet, a std::string or std::vector.
 
A

Andrey Tarasevich

zouyongbin said:
The code segment like below will fail at compilation:

int t = 5;
const int ix = t;
char cx[ix] = "hell";

while that like below is correct:

int t = 5;
const int ix = 5;
char cx[iy] = "hell";

(Assuming you meant 'ix' here).
Can anybody give me a explanation about what is really happening here?

In C++ language only integral constant expressions (ICEs) can be used to specify
size in array type declarations. I won't include the full definition of ICE here
(look it up), but what applies to this case is that if you want an object of
'const int' type to act as an ICE, you have to make sure it that it itself is
initialized with an ICE.

In the second case this requirements is met. 'ix' is initialized with an
explicit '5'' '5' is an ICE. This means that 'ix' is itself an ICE and can be
used to declare array types.

In the first case the above requirement is not met. 'iy' is initialized with 't'
and 't' is not an ICE, meaning that 'iy' is not an ICE. Therefore, 'iy' cannot
be used to declare array types.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top