Is &array[0]==(void*)&array guaranteed to be true? [duplicate]
This question already has an answer here:
why cant we pass &array to function where &array[0] is possible
6 answers
Is a pointer to an arbitrary array equal to a pointer to the array's first element after cast to void*, and independently of the compiler?
I need a template function that takes a pointer to array[0]
and gives a reference to the array. The code below does the job (at least in gcc and clang), and I would like to know if this is a legal C++ code.
My doubts are caused by the fact, that without cast to void* the code that uses the template would not compile (error message: ``static_cast from 'int ' to 'int ()[10]' is not allowed").
Thanks in advance. Here is the code:
template <std::size_t n, typename T>
inline T (& p2a( T * ptr))[n]
return *static_cast<T(*)[n]>( static_cast<void*>(ptr) );
c++ arrays
marked as duplicate by πάντα ῥεῖ
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 11 at 17:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
why cant we pass &array to function where &array[0] is possible
6 answers
Is a pointer to an arbitrary array equal to a pointer to the array's first element after cast to void*, and independently of the compiler?
I need a template function that takes a pointer to array[0]
and gives a reference to the array. The code below does the job (at least in gcc and clang), and I would like to know if this is a legal C++ code.
My doubts are caused by the fact, that without cast to void* the code that uses the template would not compile (error message: ``static_cast from 'int ' to 'int ()[10]' is not allowed").
Thanks in advance. Here is the code:
template <std::size_t n, typename T>
inline T (& p2a( T * ptr))[n]
return *static_cast<T(*)[n]>( static_cast<void*>(ptr) );
c++ arrays
marked as duplicate by πάντα ῥεῖ
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 11 at 17:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Interesting question. I don't think the standard guarantees there must be no padding at the beginning of an array, but it'd be weird if there were.
– PSkocik
Nov 11 at 17:24
Related: stackoverflow.com/questions/47522295/…
– PSkocik
Nov 11 at 17:27
add a comment |
This question already has an answer here:
why cant we pass &array to function where &array[0] is possible
6 answers
Is a pointer to an arbitrary array equal to a pointer to the array's first element after cast to void*, and independently of the compiler?
I need a template function that takes a pointer to array[0]
and gives a reference to the array. The code below does the job (at least in gcc and clang), and I would like to know if this is a legal C++ code.
My doubts are caused by the fact, that without cast to void* the code that uses the template would not compile (error message: ``static_cast from 'int ' to 'int ()[10]' is not allowed").
Thanks in advance. Here is the code:
template <std::size_t n, typename T>
inline T (& p2a( T * ptr))[n]
return *static_cast<T(*)[n]>( static_cast<void*>(ptr) );
c++ arrays
This question already has an answer here:
why cant we pass &array to function where &array[0] is possible
6 answers
Is a pointer to an arbitrary array equal to a pointer to the array's first element after cast to void*, and independently of the compiler?
I need a template function that takes a pointer to array[0]
and gives a reference to the array. The code below does the job (at least in gcc and clang), and I would like to know if this is a legal C++ code.
My doubts are caused by the fact, that without cast to void* the code that uses the template would not compile (error message: ``static_cast from 'int ' to 'int ()[10]' is not allowed").
Thanks in advance. Here is the code:
template <std::size_t n, typename T>
inline T (& p2a( T * ptr))[n]
return *static_cast<T(*)[n]>( static_cast<void*>(ptr) );
This question already has an answer here:
why cant we pass &array to function where &array[0] is possible
6 answers
c++ arrays
c++ arrays
asked Nov 11 at 17:06
user10636819
522
522
marked as duplicate by πάντα ῥεῖ
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 11 at 17:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by πάντα ῥεῖ
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 11 at 17:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Interesting question. I don't think the standard guarantees there must be no padding at the beginning of an array, but it'd be weird if there were.
– PSkocik
Nov 11 at 17:24
Related: stackoverflow.com/questions/47522295/…
– PSkocik
Nov 11 at 17:27
add a comment |
Interesting question. I don't think the standard guarantees there must be no padding at the beginning of an array, but it'd be weird if there were.
– PSkocik
Nov 11 at 17:24
Related: stackoverflow.com/questions/47522295/…
– PSkocik
Nov 11 at 17:27
Interesting question. I don't think the standard guarantees there must be no padding at the beginning of an array, but it'd be weird if there were.
– PSkocik
Nov 11 at 17:24
Interesting question. I don't think the standard guarantees there must be no padding at the beginning of an array, but it'd be weird if there were.
– PSkocik
Nov 11 at 17:24
Related: stackoverflow.com/questions/47522295/…
– PSkocik
Nov 11 at 17:27
Related: stackoverflow.com/questions/47522295/…
– PSkocik
Nov 11 at 17:27
add a comment |
1 Answer
1
active
oldest
votes
Yes, the address of an array is the same as the address of the array's first element (i.e., there is no padding at the beginning of an array) and a cast to void*
doesn't change the address, so your code does the right thing. However, it is more straightforward to write it as follows:
template <std::size_t n, typename T>
inline auto p2a(T* ptr)
return reinterpret_cast<T(&)[N]>(*ptr);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yes, the address of an array is the same as the address of the array's first element (i.e., there is no padding at the beginning of an array) and a cast to void*
doesn't change the address, so your code does the right thing. However, it is more straightforward to write it as follows:
template <std::size_t n, typename T>
inline auto p2a(T* ptr)
return reinterpret_cast<T(&)[N]>(*ptr);
add a comment |
Yes, the address of an array is the same as the address of the array's first element (i.e., there is no padding at the beginning of an array) and a cast to void*
doesn't change the address, so your code does the right thing. However, it is more straightforward to write it as follows:
template <std::size_t n, typename T>
inline auto p2a(T* ptr)
return reinterpret_cast<T(&)[N]>(*ptr);
add a comment |
Yes, the address of an array is the same as the address of the array's first element (i.e., there is no padding at the beginning of an array) and a cast to void*
doesn't change the address, so your code does the right thing. However, it is more straightforward to write it as follows:
template <std::size_t n, typename T>
inline auto p2a(T* ptr)
return reinterpret_cast<T(&)[N]>(*ptr);
Yes, the address of an array is the same as the address of the array's first element (i.e., there is no padding at the beginning of an array) and a cast to void*
doesn't change the address, so your code does the right thing. However, it is more straightforward to write it as follows:
template <std::size_t n, typename T>
inline auto p2a(T* ptr)
return reinterpret_cast<T(&)[N]>(*ptr);
answered Nov 11 at 17:23
Brian
63.9k794179
63.9k794179
add a comment |
add a comment |
Interesting question. I don't think the standard guarantees there must be no padding at the beginning of an array, but it'd be weird if there were.
– PSkocik
Nov 11 at 17:24
Related: stackoverflow.com/questions/47522295/…
– PSkocik
Nov 11 at 17:27