Why cannot build range expression passing an array as a function argument and using in a range-for-statement.
Thanks for the help
void increment(int v[]){
// No problem
int w[10] = {9,8,7,6,5,4,3,2,1,9};
for(int& x:w){
std::cout<<"range-for-statement: "<<++x<<"\n";
}
// error: cannot build range expression with array function
// parameter 'v' since parameter with array type 'int []' is
// treated as pointer type 'int *'
for(int x:v){
std::cout<<"printing "<<x<<"\n";
}
// No problem
for (int i = 0; i < 10; i++){
int* p = &v[i];
}
}
int main()
{
int v[10] = {9,8,7,6,5,4,3,2,1,9};
increment(v);
}
Despite appearances, v is a pointer not an array - as the error message says. Built-in arrays are weird things, which can't be copied or passed by value, and silently turn into pointers at awkward moments.
There is no way to know the size of the array it points to, so no way to generate a loop to iterate over it. Options include:
use a proper range-style container, like std::array or std::vector
pass the size of the array as an extra argument, and interate with an old-school loop
It's because of the way you pass the array to the function. As written it decays to pointer. Try
template<int N>
void increment(int (&v)[N])
{
for (int x : v) std::cout << "printing " << x << "\n";
}
int main()
{
int v[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 9 };
increment(v);
}
This runs because a reference to an array of N ints is passed in the function and (unlike pointers) range for loops can iterate on those.
The function parameter int v[] is adjasted to int * Pointers do not keep information whether they point a single object or the first object of a sequence of objects.
The range-based for statement in fact uses the same expressions as standard functions std::begin and std::end They cannot be defined for pointers without knowing the size of the array. They can be defined for arrays, not pointers.
Related
I have initiated an array of 6 elements and tried to print it using a function called 'print'. I have used array object from the stl library. I passed the address of the array object to the print function. When I tried to change the value of the array object in the print function I am getting mismatched types error.
#include <bits/stdc++.h>
using namespace std;
void print(array<int, 6> *arr){
for(int i : *arr){
cout<<i<<" ";
}
*(*arr+2)=2;
cout<<endl;
}
int main(){
array<int, 6> arr2={1, 2, 3, 4, 5, 6};
print(&arr2);
print(&arr2);
}
In *(*arr+2)=2; you deference the array pointer and try to add 2 to it and then dereference that result to assign 2. I assume you want to assign 2 to the element at index 2 in the array.
You do not need to use pointers here though, take the array by reference.
And, never #include <bits/stdc++.h>.
#include <array> // include the proper header files
#include <iostream>
void print(std::array<int, 6>& arr) { // by reference
for (int i : arr) {
std::cout << i << ' ';
}
arr[2] = 2; // assign 2 to the element at index 2
std::cout << '\n'; // std::endl flushes the stream which is usually uncessary
}
int main() {
std::array<int, 6> arr2 = {1, 2, 3, 4, 5, 6};
print(arr2); // and don't take the array address here
print(arr2);
}
If you really want to use a pointer here, this could be an option:
#include <array>
#include <iostream>
void print(std::array<int, 6>* arr_ptr) { // pointer
if (arr_ptr == nullptr) return; // check that it's pointing at something
std::array<int, 6>& arr = *arr_ptr; // dereference it
for (int i : arr) {
std::cout << i << ' ';
}
arr[2] = 2;
std::cout << '\n';
}
int main() {
std::array<int, 6> arr2 = {1, 2, 3, 4, 5, 6};
print(&arr2);
print(&arr2);
}
This statement
*(*arr+2)=2;
does not make a sense. For example the operator + is not defined for the class template std::array and as a result this expression *arr+2 is invalid.
You could write
( *arr )[2] = 2;
or for example
*( ( *arr ).begin() + 2 ) = 2;
*(*arr+2)=2;
Would be equivalent to
(*arr)[2] = 2;
if arr were a pointer to an array. I assume that's what you are looking for. But a std::array instance is not an array in that sense. It is an object encapsulating an array, and emulating some of the properties of the underlying array, but it cannot be used interchangeably with the underlying array. In particular, std::array objects do not decay to pointers as actual arrays do, and your code appears to be trying to rely on that.
You could instead do
*((*arr).data() + 2) = 2;
or, more idiomatically,
*(arr->data() + 2) = 2;
to leverage that array-to-pointer decay. Or you could do
arr->data()[2] = 2;
. But none of those is as clear or straightforward as the ...
(*arr)[2] = 2;
... already mentioned, which is what you should do if you must work with a pointer to a std::array instead of a reference to one.
First note that std::array has no operator+. So when you wrote the expression:
*arr+2 // INCORRECT
In the above expression, you are dereferencing the pointer to std::array and then adding 2 to the result. But as i said at the beginning, that there is no operator+ for std::array, this expression is incorrect.
Limitation
Second you program has a limitation which is that the function print can accept a pointer to an std::array with only 6 elements. So if you try to pass a pointer to an std::array of some different size then your program won't work.
Solution
You can fix these by:
Passing the std::array by reference.
Using templates. In particular, using template nontype parameter.
The advantage of using template nontype parameter is that now you can call the function print with an std::array of different size as shown below.
#include <iostream>
#include <array>
template<std::size_t N>
void print(std::array<int, N> &arr){ //by reference
for(const int &i : arr){
std::cout<<i<<" ";
}
arr.at(2) = 2; //use at() member function so that you don't get undefined behavior
std::cout<<std::endl;
}
int main(){
std::array<int, 6> arr2={1, 2, 3, 4, 5, 6};
print(arr2);
std::array<int, 10> arr3 = {1,2,4,3,5,6,7,8,9,10};
print(arr3);
}
Some of the modifications that i made include:
Removed #include <bits/stdc++.h> and only included headers that are needed.
Used templates. In particular, removed the limitation by using template nontype parameter.
Passed the std::array by reference. That is, there is no need to pass a pointer to an std::array. We can just pass the std::array by reference.
Used at() member function so that we don't go out of bounds and so that we don't get undefined behavior.
I am trying to write a function that prints out the elements in an array. However when I work with the arrays that are passed, I don't know how to iterate over the array.
void
print_array(int* b)
{
int sizeof_b = sizeof(b) / sizeof(b[0]);
int i;
for (i = 0; i < sizeof_b; i++)
{
printf("%d", b[i]);
}
}
What is the best way to do iterate over the passed array?
You need to also pass the size of the array to the function.
When you pass in the array to your function, you are really passing in the address of the first element in that array. So the pointer is only pointing to the first element once inside your function.
Since memory in the array is continuous though, you can still use pointer arithmetic such as (b+1) to point to the second element or equivalently b[1]
void print_array(int* b, int num_elements)
{
for (int i = 0; i < num_elements; i++)
{
printf("%d", b[i]);
}
}
This trick only works with arrays not pointers:
sizeof(b) / sizeof(b[0])
... and arrays are not the same as pointers.
Why don't you use function templates for this (C++)?
template<class T, int N> void f(T (&r)[N]){
}
int main(){
int buf[10];
f(buf);
}
EDIT 2:
The qn now appears to have C tag and the C++ tag is removed.
For C, you have to pass the length (number of elements)of the array.
For C++, you can pass the length, BUT, if you have access to C++0x, BETTER is to use std::array. See here and here. It carries the length, and provides check for out-of-bound if you access elements using the at() member function.
In C99, you can require that an array an array has at least n elements thusly:
void print_array(int b[static n]);
6.7.5.3.7: A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to
type’’, where the type qualifiers (if any) are those specified within the [ and ] of the
array type derivation. If the keyword static also appears within the [ and ] of the
array type derivation, then for each call to the function, the value of the corresponding
actual argument shall provide access to the first element of an array with at least as many
elements as specified by the size expression.
In GCC you can pass the size of an array implicitly like this:
void print_array(int n, int b[n]);
You could try this...
#include <cstdio>
void
print_array(int b[], size_t N)
{
for (int i = 0; i < N; ++i)
printf("%d ", b[i]);
printf("\n");
}
template <size_t N>
inline void
print_array(int (&b)[N])
{
// could have loop here, but inline forwarding to
// single function eliminates code bloat...
print_array(b, N);
}
int main()
{
int a[] = { 1, 2 };
int b[] = { };
int c[] = { 1, 2, 3, 4, 5 };
print_array(a);
// print_array(b);
print_array(c);
}
...interestingly b doesn't work...
array_size.cc: In function `int main()':
array_size.cc:19: error: no matching function for call to `print_array(int[0u])'
JoshD points out in comments below the issue re 0 sized arrays (a GCC extension), and the size inference above.
In c++ you can also use a some type of list class implemented as an array with a size method or as a struct with a size member(in c or c++).
Use variable to pass the size of array.
int sizeof_b = sizeof(b) / sizeof(b[0]); does nothing but getting the pre-declared array size, which is known, and you could have passed it as an argument; for instance, void print_array(int*b, int size). size could be the user-defined size too.
int sizeof_b = sizeof(b) / sizeof(b[0]); will cause redundant iteration when the number of elements is less than the pre-declared array-size.
The question has already some good answers, for example the second one. However there is a lack of explanation so I would like to extend the sample and explain it:
Using template and template parameters and in this case None-Type Template parameters makes it possible to get the size of a fixed array with any type.
Assume you have such a function template:
template<typename T, int S>
int getSizeOfArray(T (&arr)[S]) {
return S;
}
The template is clearly for any type(here T) and a fixed integer(S).
The function as you see takes a reference to an array of S objects of type T, as you know in C++ you cannot pass arrays to functions by value but by reference so the function has to take a reference.
Now if u use it like this:
int i_arr[] = { 3, 8, 90, -1 };
std::cout << "number f elements in Array: " << getSizeOfArray(i_arr) << std::endl;
The compiler will implicitly instantiate the template function and detect the arguments, so the S here is 4 which is returned and printed to output.
This question already has answers here:
When a function has a specific-size array parameter, why is it replaced with a pointer?
(3 answers)
Closed 7 years ago.
I am aware that an array can be passed to a function in quite a few ways.
#include <iostream>
#include <utility>
using namespace std;
pair<int, int> problem1(int a[]);
int main()
{
int a[] = { 10, 7, 3, 5, 8, 2, 9 };
pair<int, int> p = problem1(a);
cout << "Max =" << p.first << endl;
cout << "Min =" << p.second << endl;
getchar();
return 0;
}
pair<int,int> problem1(int a[])
{
int max = a[0], min = a[0], n = sizeof(a) / sizeof(int);
for (int i = 1; i < n; i++)
{
if (a[i]>max)
{
max = a[i];
}
if (a[i] < min)
{
min = a[i];
}
}
return make_pair(max,min);
}
My code above passes only the first element while it should be passing an array (or technically, a pointer to the array) and hence, the output is 10, 10 for both max and min (i.e. a[0] only).
What am I doing wrong, I guess this is the correct way.
The contents of the array are being passed to the function. The problem is:
n = sizeof(a) / sizeof(int)
Does not give you the size of the array. Once you pass an array to a function you can't get its size again.
Since you aren't using a dynamic array you can use a std::array which does remember its size.
You could also use:
template <int N>
void problem1(int (&a) [N])
{
int size = N;
//...
}
No, you simply cannot pass an array as a parameter in C or C++, at least not directly.
In this declaration:
pair<int, int> problem1(int a[]);
even though a appears to be defined as an array, the declaration is "adjusted" to a pointer to the element type, so the above really means:
pair<int, int> problem1(int* a);
Also, an expression of array type is, in most contexts, implicitly converted to a pointer to the array's initial element. (Exceptions include an array as the operand of sizeof or unary &). So in a call to the above function:
int arr[10];
problem1(arr);
the array expression arr is equivalent to &arr[0], and that address (pointer value) is what's passed to the function.
Of course you can write code that does the equivalent of passing an array. You can make the array a member of a structure (but then it has to be of fixed length). Or you can pass a pointer to the initial element and pass a separate parameter containing the actual length of the array object.
Or you can use one of the C++ standard library classes that implement array-like data structures; then the length can be taken directly from the parameter.
I highly recommend reading section 6 of the comp.lang.c FAQ, which covers arrays and pointers. It's applicable to C++ as well (though it doesn't mention the C++ standard library).
In C++ language a function parameter declared as int a[] is immediately interpreted as and is equivalent to int *a parameter. Which means that you are not passing an array to your function. You are passing a pointer to the first element of an array.
Trying to apply the sizeof(a) / sizeof(int) technique to a pointer is useless. It cannot possibly produce the size of the argument arraay.
One alternative that hasn't been mentioned is writing your code as a template, and passing the array by reference so the template can deduce the size of the array:
template <class T, size_t n>
pair<T, T> problem1(T(&a)[n]) {
T max = a[0], min = a[0];
for (size_t i = 1; i < n; i++) {
if (a[i]>max) {
max = a[i];
}
if (a[i] < min) {
min = a[i];
}
}
return make_pair(max, min);
}
Note, however, that this will only work if you pass a real array, not a pointer. For example, code like this:
int *b = new int[10];
for (int i = 0; i < 10; i++)
b[i] = rand();
auto result = problem1(b);
...won't compile at all (because we've defined problem1 to receive a reference to an array, and b is a pointer, not an array).
I am trying to pass a 2-d array to a function which accept a pointer to pointer. And I have learnt that a 2-d array is nothing a pointer to pointer(pointer to 1-D array). I when I compile the below code I got this error.
#include<iostream>
void myFuntion(int **array)
{
}
int main()
{
int array[][]= {{1,2,3,4},{5,6,7,8,9},{10,11,12,13}};
myFuntion(array);
return 0;
}
In function 'int main()':
Line 5: error: declaration of 'array' as multidimensional array must have bounds for all dimensions except the first
compilation terminated due to -Wfatal-errors.
Can anybody clear my doubt regarding this and some docs if possible for my more doubts.
void myFunction(int arr[][4])
you can put any number in the first [] but the compiler will ignore it. When passing a vector as parameter you must specify all dimensions but the first one.
You should at least specify the size of your second dimension.
int array[][5] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 }, { 10, 11, 12, 13 } };
There is also an error which is often repeated. To pass a 2D array as argument, you have to use the following types:
void myFuntion(int (*array)[SIZE2]);
/* or */
void myFuntion(int array[SIZE1][SIZE2]);
Why don't use std::vector instead of "raw" arrays. Advantages:
1. It can dynamically grow.
2. There is no issues about passing arguments to the function. I.e. try to call void myFuntion(int array[SIZE1][SIZE2]); with array, that has some different sizes not SIZE1 and SIZE2
Another templated solution would be:
template<int M, int N>
void myFunction(int array[N][M])
{
}
#include<iostream>
void myFuntion(int arr[3][4]);
int main()
{
int array[3][4]= {{1,2,3,4},{5,6,7,8},{10,11,12,13}};
myFuntion(array);
return 0;
}
void myFuntion(int arr[3][4])
{
}
http://liveworkspace.org/code/0ae51e7f931c39e4f54b1ca36441de4e
declaration of ‘array’ as multidimensional array must have bounds for all dimensions except the first
So you have to give
array[][size] //here you must to give size for 2nd or more
For passing the array in function , array is not a pointer to a pointer but it's pointer to an array so you write like this
fun(int (*array)[])
Here if you miss the parenthesis around (*array) then it will be an array of pointers
because of precedence of operators [] has higher precedence to *
How do I reliably get the size of a C-style array? The method often recommended seems to be to use sizeof, but it doesn't work in the foo function, where x is passed in:
#include <iostream>
void foo(int x[]) {
std::cerr << (sizeof(x) / sizeof(int)); // 2
}
int main(){
int x[] = {1,2,3,4,5};
std::cerr << (sizeof(x) / sizeof(int)); // 5
foo(x);
return 0;
}
Answers to this question recommend sizeof but they don't say that it (apparently?) doesn't work if you pass the array around. So, do I have to use a sentinel instead? (I don't think the users of my foo function can always be trusted to put a sentinel at the end. Of course, I could use std::vector, but then I don't get the nice shorthand syntax {1,2,3,4,5}.)
In C array parameters in C are really just pointers so sizeof() won't work. You either need to pass in the size as another parameter or use a sentinel - whichever is most appropriate for your design.
Some other options:
Some other info:
for C++, instead of passing a raw array pointer, you might want to have the parameter use something that wraps the array in a class template that keeps track of the array size and provides methods to copy data into the array in a safe manner. Something like STLSoft's array_proxy template or Boost's boost::array might help. I've used an array_proxy template to nice effect before. Inside the function using the parameter, you get std::vector like operations, but the caller of the function can be using a simple C array. There's no copying of the array - the array_proxy template takes care of packaging the array pointer and the array's size nearly automatically.
a macro to use in C for determining the number of elements in an array (for when sizeof() might help - ie., you're not dealing with a simple pointer): Is there a standard function in C that would return the length of an array?
A common idiom mentioned in GNU Libstdc++ documentation is the lengthof function:
template<typename T, unsigned int sz>
inline unsigned int lengthof(T (&)[sz]) { return sz; }
You can use it as
int x[] = {1,2,3,4,5};
std::cerr << lengthof(x) << std::endl;
Warning: this will work only when the array has not decayed into a pointer.
How about this?..
template <int N>
void foo(int (&x)[N]) {
std::cerr << N;
}
You can either pass the size around, use a sentinel or even better use std::vector. Even though std::vector lacks initializer lists it is still easy to construct a vector with a set of elements (although not quite as nice)
static const int arr[] = {1,2,3,4,5};
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
The std::vector class also makes making mistakes far harder, which is worth its weight in gold. Another bonus is that all C++ should be familiar with it and most C++ applications should be using a std::vector rather than a raw C array.
As a quick note, C++0x adds Initializer lists
std::vector<int> v = {1, 2, 3, 4};
You can also use Boost.Assign to do the same thing although the syntax is a bit more convoluted.
std::vector<int> v = boost::assign::list_of(1)(2)(3)(4);
or
std::vector<int> v;
v += 1, 2, 3, 4;
c provides no native support for this. Once an array is passed out of its declared scope, its size is lost.
You can pass the size with the array. You can even bundle them into a structure if you always to to keep the size, though you'll have some bookkeepping overhead with that.
I also agree that Corwin's method above is very good.
template <int N>
void foo(int (&x)[N])
{
std::cerr << N;
}
I don't think anybody gave a really good reason why this is not a good idea.
In java, for example, we can write things like:
int numbers [] = {1, 2, 3, 4};
for(int i = 0; i < numbers.length(); i++)
{
System.out.println(numbers[i]+"\n");
}
In C++ it would be nice instead of saying
int numbers [] = {1, 2, 3, 4};
int size = sizeof(numbers)/sizeof(int);
for(int i = 0; i < size; i++)
{
cout << numbers[i] << endl;
}
We could take it a step further and go
template <int N>
int size(int (&X)[N])
{
return N;
}
Or if that causes problems I suppose you could write explicitly:
template < int N >
int size(int (&X)[N])
{
int value = (sizeof(X)/sizeof(X[0]));
return value;
}
Then we just have to go in main:
int numbers [] = {1, 2, 3, 4};
for(int i = 0; i < size(numbers); i++)
{
cout << numbers[i] << endl;
}
makes sense to me :-)
An array expression will have its type implicitly converted from "N-element array of T" to "pointer to T" and its value will be the address of the first element in the array, unless the array expression is the operand of either the sizeof or address-of (&) operators, or if the array expression is a string literal being used to initialize another array in a declaration. In short, you can't pass an array to a function as an array; what the function receives is a pointer value, not an array value.
You have to pass the array size as a separate parameter.
Since you're using C++, use vectors (or some other suitable STL container) instead of C-style arrays. Yes, you lose the handy shorthand syntax, but the tradeoff is more than worth it. Seriously.
Now, you can use C++11's extent and rank.
By example:
#include <iostream>
#include <type_traits>
int main()
{
int a[][3] = {{1, 2, 3}, {4, 5, 6}};
std::cout << "\nRank: : " << std::rank<decltype(a)>::value;
std::cout << "\nSize: [_here_][]: " << std::extent<decltype(a), 0>::value;
std::cout << "\nSize: [][_here_]: " << std::extent<decltype(a), 1>::value;
std::cout << "\nSize: [][]_here_: " << std::extent<decltype(a), 2>::value;
}
prints:
Rank: : 2
Size: [_here_][]: 2
Size: [][_here_]: 3
Size: [][]_here_: 0
You need to pass the size along with the array, just like it is done in many library functions, for instance strncpy(), strncmp() etc. Sorry, this is just the way it works in C:-).
Alternatively you could roll out your own structure like:
struct array {
int* data;
int size;
};
and pass it around your code.
Of course you can still use std::list or std::vector if you want to be more C++ -ish.
Since c++11, there is a very convenient way:
static const int array[] = { 1, 2, 3, 6 };
int size = (int)std::distance(std::begin(array), std::end(array))+1;