OAH
OAH

Reputation: 335

C++ store char array in int pointer

Is there any type of casting (implicit, explicit) done in this line:

int *p= "hello there"; 

Moreover what is correctness of such a statement from C/C++ point of view ?

Note: it compiles on AVR-GCC and fails with other compilers.

Upvotes: 2

Views: 220

Answers (3)

M.M
M.M

Reputation: 141598

This code is illegal in C and C++. We might guess that AVR-GCC pretends that you had written:

int *p = (int *)"hello there";

You could perhaps try to confirm this by writing:

char *q = "hello there";
int *p = q;

and seeing if p and q hold the same address. (although that would not be completely conclusive either way).

It would be better to find out how to invoke your compiler in a mode where it will report errors or warnings for illegal code. Normally for gcc, -Wall -pedantic does this, and usually you'd also specify a standard version such as -std=c99 or -std=gnu99 .


Supposing the code is treated as int *p = (int *)"hello there";. This code may cause undefined or unexpected behaviour because the char array might not be correctly aligned for int.

Also, it will cause undefined behaviour if you read or write through p due to violating the strict aliasing rule, which says that reading or writing as int is only permitted for variables declared as int, or malloc'd space which (if being read) already has had an int written.

Upvotes: 1

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

"Is there any type of casting (implicit, explicit) done in this line:"

int *p= "hello there"; 

No, there isn't any casting done.

But in C++ you can use a reinterpret_cast<int*> along with a const_cast<char*> like follows:

int *p= reinterpret_cast<int*>(const_cast<char*>("hello there")); 

See a working demo.

"Moreover what do you think about the correctness of such statement from (C,C++) point of view."

Well, that's totally left upon the user of such statement. It's certainly not recommendable to do so (because you're hitting not specified1 behavior), and dangerous if you're not a 100% sure about what you're doing.

But at least both languages support to do so.

The a bit shorter variant for C is:

int *p= (int*)"hello there";

"Note: it compiles on AVR-GCC and fails with other compilers."

Well, I don't know the version you're using for the AVR cross toolchain, but any halfway up-to-date version of GCC should give an error for this statement like

prog.cpp:3: error: cannot convert 'const char*' to 'int*' in initialization

See demo please

I'm using a fairly old version of GCC to make up that demo: g++ 4.3.2. The latest one is 5.x if I'm informed correctly, and I'm pretty sure that it's possible to build a cross toolchain for AVR with it.


1 Which is subtly different from undefined behavior. Just depends on what your hardware goes along with these pointer addresses, and the alignment settings.

Upvotes: 6

pm100
pm100

Reputation: 50190

to answer your question (ignoring the 'why do it' comments - which are correct)

You do

int *p=(int*)"hello there";

Upvotes: 1

Related Questions