Passing Unsized Array through function in C++ [duplicate]










0
















This question already has an answer here:



  • passing an array as a const argument of a method in C++

    4 answers



I'm trying to initialize a dynamic array and pass it through a function, but I keep getting errors every-time I do so.



float addLayer(float layers)
float addColor = 0;
if (std::find(std::begin(layers), std::end(layers), addColor))
// run code
addColor = ...;

return addColor


float layers = 0;
newColor = addLayer(layers); //line 2


Error I receive:



Expected identifier or '(' on line 2


Any help would be appreciated, thank you










share|improve this question













marked as duplicate by Jarod42 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 15 '18 at 9:51


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.













  • 1





    C++ does not have dynamic arrays. Use a vector instead. Also, you do not need to include the brackets for arrays passed as function arguments.

    – James
    Nov 15 '18 at 2:37
















0
















This question already has an answer here:



  • passing an array as a const argument of a method in C++

    4 answers



I'm trying to initialize a dynamic array and pass it through a function, but I keep getting errors every-time I do so.



float addLayer(float layers)
float addColor = 0;
if (std::find(std::begin(layers), std::end(layers), addColor))
// run code
addColor = ...;

return addColor


float layers = 0;
newColor = addLayer(layers); //line 2


Error I receive:



Expected identifier or '(' on line 2


Any help would be appreciated, thank you










share|improve this question













marked as duplicate by Jarod42 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 15 '18 at 9:51


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.













  • 1





    C++ does not have dynamic arrays. Use a vector instead. Also, you do not need to include the brackets for arrays passed as function arguments.

    – James
    Nov 15 '18 at 2:37














0












0








0









This question already has an answer here:



  • passing an array as a const argument of a method in C++

    4 answers



I'm trying to initialize a dynamic array and pass it through a function, but I keep getting errors every-time I do so.



float addLayer(float layers)
float addColor = 0;
if (std::find(std::begin(layers), std::end(layers), addColor))
// run code
addColor = ...;

return addColor


float layers = 0;
newColor = addLayer(layers); //line 2


Error I receive:



Expected identifier or '(' on line 2


Any help would be appreciated, thank you










share|improve this question















This question already has an answer here:



  • passing an array as a const argument of a method in C++

    4 answers



I'm trying to initialize a dynamic array and pass it through a function, but I keep getting errors every-time I do so.



float addLayer(float layers)
float addColor = 0;
if (std::find(std::begin(layers), std::end(layers), addColor))
// run code
addColor = ...;

return addColor


float layers = 0;
newColor = addLayer(layers); //line 2


Error I receive:



Expected identifier or '(' on line 2


Any help would be appreciated, thank you





This question already has an answer here:



  • passing an array as a const argument of a method in C++

    4 answers







c++ function dynamic-arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 2:35









Cristian Cristian

90115




90115




marked as duplicate by Jarod42 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 15 '18 at 9:51


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 Jarod42 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 15 '18 at 9:51


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.









  • 1





    C++ does not have dynamic arrays. Use a vector instead. Also, you do not need to include the brackets for arrays passed as function arguments.

    – James
    Nov 15 '18 at 2:37













  • 1





    C++ does not have dynamic arrays. Use a vector instead. Also, you do not need to include the brackets for arrays passed as function arguments.

    – James
    Nov 15 '18 at 2:37








1




1





C++ does not have dynamic arrays. Use a vector instead. Also, you do not need to include the brackets for arrays passed as function arguments.

– James
Nov 15 '18 at 2:37






C++ does not have dynamic arrays. Use a vector instead. Also, you do not need to include the brackets for arrays passed as function arguments.

– James
Nov 15 '18 at 2:37













2 Answers
2






active

oldest

votes


















2














float addLayer(float layers);


is equivalent to



float addLayer(float* layers);


i. e. all you have is the pointer. All length information is lost as soon as the array decays to the pointer.



To retain length information, you can pass it in a separate parameter, alternatively, you can pass a reference to array:



template <size_t N>
float addLayer(float(&layers)[N]); // pass reference to array of length N


Additionally, there's a syntax error:



newColor = addLayer(layers);
// ^^


Layers is already an array (but decays to pointer if passed to pointer version of function), and what you actually do in above line is applying the index operator to the array - however without argument (note that with argument, you'd get a float value, not a pointer any more).



Finally: Both std::array(fixed size) and std::vector(variable size) are better alternatives to raw arrays, prefer using one of these whenever possible.






share|improve this answer
































    1














    The problem is you can't pass a C array as an argument to a function -- C does not allow it, so C++ does not either. If you declare a function as taking a C array as a parameter, the compiler silently changes it into a pointer, so you're actually passing a pointer. Thus, calling std::begin and std::end on that pointer argument won't work.



    In order to make this work in C++, you need to use a std::array or std::vector instead.






    share|improve this answer





























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      float addLayer(float layers);


      is equivalent to



      float addLayer(float* layers);


      i. e. all you have is the pointer. All length information is lost as soon as the array decays to the pointer.



      To retain length information, you can pass it in a separate parameter, alternatively, you can pass a reference to array:



      template <size_t N>
      float addLayer(float(&layers)[N]); // pass reference to array of length N


      Additionally, there's a syntax error:



      newColor = addLayer(layers);
      // ^^


      Layers is already an array (but decays to pointer if passed to pointer version of function), and what you actually do in above line is applying the index operator to the array - however without argument (note that with argument, you'd get a float value, not a pointer any more).



      Finally: Both std::array(fixed size) and std::vector(variable size) are better alternatives to raw arrays, prefer using one of these whenever possible.






      share|improve this answer





























        2














        float addLayer(float layers);


        is equivalent to



        float addLayer(float* layers);


        i. e. all you have is the pointer. All length information is lost as soon as the array decays to the pointer.



        To retain length information, you can pass it in a separate parameter, alternatively, you can pass a reference to array:



        template <size_t N>
        float addLayer(float(&layers)[N]); // pass reference to array of length N


        Additionally, there's a syntax error:



        newColor = addLayer(layers);
        // ^^


        Layers is already an array (but decays to pointer if passed to pointer version of function), and what you actually do in above line is applying the index operator to the array - however without argument (note that with argument, you'd get a float value, not a pointer any more).



        Finally: Both std::array(fixed size) and std::vector(variable size) are better alternatives to raw arrays, prefer using one of these whenever possible.






        share|improve this answer



























          2












          2








          2







          float addLayer(float layers);


          is equivalent to



          float addLayer(float* layers);


          i. e. all you have is the pointer. All length information is lost as soon as the array decays to the pointer.



          To retain length information, you can pass it in a separate parameter, alternatively, you can pass a reference to array:



          template <size_t N>
          float addLayer(float(&layers)[N]); // pass reference to array of length N


          Additionally, there's a syntax error:



          newColor = addLayer(layers);
          // ^^


          Layers is already an array (but decays to pointer if passed to pointer version of function), and what you actually do in above line is applying the index operator to the array - however without argument (note that with argument, you'd get a float value, not a pointer any more).



          Finally: Both std::array(fixed size) and std::vector(variable size) are better alternatives to raw arrays, prefer using one of these whenever possible.






          share|improve this answer















          float addLayer(float layers);


          is equivalent to



          float addLayer(float* layers);


          i. e. all you have is the pointer. All length information is lost as soon as the array decays to the pointer.



          To retain length information, you can pass it in a separate parameter, alternatively, you can pass a reference to array:



          template <size_t N>
          float addLayer(float(&layers)[N]); // pass reference to array of length N


          Additionally, there's a syntax error:



          newColor = addLayer(layers);
          // ^^


          Layers is already an array (but decays to pointer if passed to pointer version of function), and what you actually do in above line is applying the index operator to the array - however without argument (note that with argument, you'd get a float value, not a pointer any more).



          Finally: Both std::array(fixed size) and std::vector(variable size) are better alternatives to raw arrays, prefer using one of these whenever possible.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 2:53

























          answered Nov 15 '18 at 2:41









          AconcaguaAconcagua

          12.7k32144




          12.7k32144























              1














              The problem is you can't pass a C array as an argument to a function -- C does not allow it, so C++ does not either. If you declare a function as taking a C array as a parameter, the compiler silently changes it into a pointer, so you're actually passing a pointer. Thus, calling std::begin and std::end on that pointer argument won't work.



              In order to make this work in C++, you need to use a std::array or std::vector instead.






              share|improve this answer



























                1














                The problem is you can't pass a C array as an argument to a function -- C does not allow it, so C++ does not either. If you declare a function as taking a C array as a parameter, the compiler silently changes it into a pointer, so you're actually passing a pointer. Thus, calling std::begin and std::end on that pointer argument won't work.



                In order to make this work in C++, you need to use a std::array or std::vector instead.






                share|improve this answer

























                  1












                  1








                  1







                  The problem is you can't pass a C array as an argument to a function -- C does not allow it, so C++ does not either. If you declare a function as taking a C array as a parameter, the compiler silently changes it into a pointer, so you're actually passing a pointer. Thus, calling std::begin and std::end on that pointer argument won't work.



                  In order to make this work in C++, you need to use a std::array or std::vector instead.






                  share|improve this answer













                  The problem is you can't pass a C array as an argument to a function -- C does not allow it, so C++ does not either. If you declare a function as taking a C array as a parameter, the compiler silently changes it into a pointer, so you're actually passing a pointer. Thus, calling std::begin and std::end on that pointer argument won't work.



                  In order to make this work in C++, you need to use a std::array or std::vector instead.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 2:40









                  Chris DoddChris Dodd

                  81.7k681160




                  81.7k681160













                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3