How to return a pointer to an array?

M

mani

int (*nVar)[10]..This is the variable i used in a function.. i
tried ...nothing worked.. anyone please tell me how to return it....
 
S

Salt_Peter

int (*nVar)[10]..This is the variable i used in a function.. i
tried ...nothing worked.. anyone please tell me how to return it....

Show minimum, compileable code please.
We don't know if what you have is a local array and a dangling
pointer.
Prefer const references. Prefer std::vector<> over fixed arrays.

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

template< typename T >
void foo( const std::vector< T >& r_v )
{
std::copy( r_v.begin(),
r_v.end(),
std::eek:stream_iterator< T >(std::cout, " ") );
std::cout << std::endl;
}

int main()
{
std::vector< int > vn(10, 99);
foo( vn );
}

/*
99 99 99 99 99 99 99 99 99 99
*/

The above should really be an operator<<
If you prefer working with dumb fixed arrays:

template< typename T, const std::size_t Size >
void bar( const T (& r_a)[ Size ] )
{
for( std::size_t i = 0; i < Size; ++i)
{
std::cout << r_a;
std::cout << " ";
}
std::cout << std::endl;
}

int main()
{
int array[10] = { 0 };
bar( array );
}

/*
0 0 0 0 0 0 0 0 0 0
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top