Passing Unsized Array through function in C++ [duplicate]
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
c++ function dynamic-arrays
marked as duplicate by Jarod42
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.
add a comment |
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
c++ function dynamic-arrays
marked as duplicate by Jarod42
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
add a comment |
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
c++ function dynamic-arrays
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
c++ function dynamic-arrays
asked Nov 15 '18 at 2:35
Cristian Cristian
90115
90115
marked as duplicate by Jarod42
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
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 15 '18 at 2:53
answered Nov 15 '18 at 2:41
AconcaguaAconcagua
12.7k32144
12.7k32144
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 15 '18 at 2:40
Chris DoddChris Dodd
81.7k681160
81.7k681160
add a comment |
add a comment |
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