473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to return a pointer to an array?

int (*nVar)[10]..This is the variable i used in a function.. i
tried ...nothing worked.. anyone please tell me how to return it....
Oct 18 '08 #1
3 8699
On Oct 18, 4:56*pm, mani <manigand...@gmail.comwrote:
int (*nVar)[10]..This is the variable i used in a function.. i
tried ...nothing worked.. anyone please tell me how to return it....
Just return nVar.....,
Oct 18 '08 #2
On Oct 18, 7:56 am, mani <manigand...@gmail.comwrote:
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::ostream_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[i];
std::cout << " ";
}
std::cout << std::endl;
}

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

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

Oct 18 '08 #3
In article
<2b**********************************@40g2000prx.g ooglegroups.com>, mani
<ma*********@gmail.comwrote:
int (*nVar)[10]..This is the variable i used in a function.. i
tried ...nothing worked.. anyone please tell me how to return it....
I recommend avoiding C-style arrays, especially pointers to them, as
you'll quickly get lost in the details. But if you insist, this code shows
how:

#include <iostream>

typedef int i10 [10];

i10* func()
{
i10* p = new i10 [1]; // int (*p) [10] = new int [1] [10]
(*p) [0] = 1234;
// ...
(*p) [9] = 5678;
return p;
}

int main()
{
i10* p = func();
std::cout << (*p) [0] << '\n';
// ...
std::cout << (*p) [9] << '\n';
delete [] p;
}
Oct 18 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bryan Parkoff | last post by:
I want to allocate pointer array into memory so pointer array contains ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks like below for example. unsigned char* A = new...
4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
6
by: Neo | last post by:
Dear All, I want to know how a subroutine should return an array of values to the main program. From the main program, I call a sub-routine 'get_sql' which then fetches data from oracle db using...
5
by: Brett | last post by:
Sorry for the rookie question.... If I have a pointer array (for example): char *colors = { "blue", "green" }; and I want to add yellow to this array later in my code....how would I do
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
1
by: MyCGal | last post by:
Hi, I have this code I wrote to copy any length lines into array of pointers. The problem is after storing the individual lines into the char pointer array, the dispaly() chops off some lines...
19
by: Andrew Gentile | last post by:
Hello, I have been working on a program where I need to have a function return an array. I found out that C doesn't do this, so now I am trying to get the function to return a pointer to an...
1
by: Nip | last post by:
Hello eyerybody I have got an array of objects of the same class. These objects might be for example visible or not. Because of the performance I donot want to check these properties with...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
13
by: pereges | last post by:
Hi, can some one please tell me why this program is not able to function properly. I have a array a and i am trying to create a pointer array b which points to elements less than 40 in a. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.