Is &array[0]==(void*)&array guaranteed to be true? [duplicate]











up vote
1
down vote

favorite













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) );
}









share|improve this question













marked as duplicate by πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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















up vote
1
down vote

favorite













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) );
}









share|improve this question













marked as duplicate by πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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













up vote
1
down vote

favorite









up vote
1
down vote

favorite












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) );
}









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 17:06









user10636819

472




472




marked as duplicate by πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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


















  • 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












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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);
}





share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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);
    }





    share|improve this answer

























      up vote
      0
      down vote













      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);
      }





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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);
        }





        share|improve this answer












        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);
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 17:23









        Brian

        63.7k794179




        63.7k794179















            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues