Suppressing automatic string conversion in Cling


ROOT Version: 6.20/02
Platform: macOS 10.15.4
Compiler: Clang 11.0.3


Q1. Is there any easy way to suppress the automatic conversion from char* to C string in the example below?

root [0] char str[] {"Hello"}
(char [6]) "Hello"
root [1] *(str + 1)
(char) 'e'
root [2] str + 2
(char *) "llo"

I would like to use the ROOT prompt to educate undergraduate students and to tell them how pointes work in C/C++. In line [2] above, I would like to show the address of str + 2 instead of "loo". The automatic C string conversion in this case is too user friendly for my specific education purpose.

One possible workaround is casting to void*, while at this stage I have not told the students what cast is.

root [3] void* p1 = (void*)str
(void *) 0x1119c0658
root [4] auto p2 = (void*)str
(void *) @0x7ffee1771f18
root [5] p1 == p2
(bool) true

Q2. What is @0x7ffee1771f18 in this example? p1 and p2 are identical when compared with ==, but the two different output 0x1119c0658 and @0x7ffee1771f18 are seen.


Using Form or printf may be another workaround.

root [6] printf("%p", str + 2)
0x1119c065a

@Axel @vgvassilev can you please help here? Thanks!

root [9] void *p = (void*)(str+2)
(void *) 0x105ce20ca

One if the pointer value while the other is the pointer variable address.

Q1: I can’t think of a way without a cast, and while keeping them char*. printf might indeed be the best option.
Q2: @ signals that it’s an lvalue, and it shows the address of the lvalue reference - in this case the address of p2.

Thank you all.

in this case the address of p2 .

I don’t still understand what @ means.

root [0] char str[] {"Hello"}
(char [6]) "Hello"
root [1] void* p1 = (void*)str
(void *) 0x10c054658
root [2] auto p2 = (void*)str
(void *) @0x7ffee70ddf28
root [3] &p1
(void **) 0x10c0547a0
root [4] &p2
(void **) 0x10c054880

where p2 points to 0x7ffee70ddf28 but its address is 0x10c054880.

Hi Akira!

I think you’re rightfully confused - I believe this is https://sft.its.cern.ch/jira/browse/ROOT-9687

Axel

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.