By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,110 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,110 IT Pros & Developers. It's quick & easy.

array pointers

P: n/a
cdg
Would anyone explain how to return an array that wasn`t passed from
"main" using a pointer. I'm not understanding how scope and memory are
involved in this.
However, I do know that there are two diferent ways to accomplish this
other than by pointers. One is by passing the array from "main" onto the
other functions, then just keep passing it along. And the other is to make a
it a global.
If possible, just correct the example program using a pointer.

#include <iostream>
using namespace std;

int ArrayFunctionOne();
int ArrayFunctionTwo(int []);

void main()
{
int i(0);
int array[20]={0};

array[20] = ArrayFunctionOne();

for(i=0; i<20; i++)
cout<<array[i]<<" "<<endl;

}

int ArrayFunctionOne()
{
int i(0);
int array[20]={0};

for(i=0; i<20; i++)
array[i] = i;

array[20] = ArrayFunctionTwo(array);

return array[i];

}

int ArrayFunctionTwo(int array[])
{
int i(0);
return array[i];
}
Mar 13 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
* cdg:
Would anyone explain how to return an array that wasn`t passed from
"main" using a pointer. I'm not understanding how scope and memory are
involved in this.
However, I do know that there are two diferent ways to accomplish this
other than by pointers. One is by passing the array from "main" onto the
other functions, then just keep passing it along. And the other is to make a
it a global.
If possible, just correct the example program using a pointer.
It seems reasonable to assume that you really want your
'ArrayFunctionOne' to return an array.

For that, use std::vector.

Don't dabble in pointers until you have passed the stage of writing
'void main', and preferentially wait quite a while longer.

#include <iostream>
using namespace std;

int ArrayFunctionOne();
int ArrayFunctionTwo(int []);

void main()
{
int i(0);
int array[20]={0};

array[20] = ArrayFunctionOne();

for(i=0; i<20; i++)
cout<<array[i]<<" "<<endl;

}

int ArrayFunctionOne()
{
int i(0);
int array[20]={0};

for(i=0; i<20; i++)
array[i] = i;

array[20] = ArrayFunctionTwo(array);

return array[i];

}

int ArrayFunctionTwo(int array[])
{
int i(0);
return array[i];
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 13 '06 #2

P: n/a
cdg wrote:
Would anyone explain how to return an array that wasn`t passed from
"main" using a pointer. I'm not understanding how scope and memory are
involved in this.
However, I do know that there are two diferent ways to accomplish this
other than by pointers. One is by passing the array from "main" onto the
other functions, then just keep passing it along.
Still you need to pass it somehow, either throuh a reference or a pointer.
Arrays are not copyable, so they cannot be passed by value.
And the other is to make a it a global.
If possible, just correct the example program using a pointer.

#include <iostream>
using namespace std;

int ArrayFunctionOne();
int ArrayFunctionTwo(int []);
This function takes a pointer to int as argument.
void main()
main() must return int.
{
int i(0);
int array[20]={0};

array[20] = ArrayFunctionOne();

for(i=0; i<20; i++)
cout<<array[i]<<" "<<endl;

}

int ArrayFunctionOne()
{
int i(0);
int array[20]={0};

for(i=0; i<20; i++)
array[i] = i;

array[20] = ArrayFunctionTwo(array);

return array[i];

}

int ArrayFunctionTwo(int array[])
{
int i(0);
return array[i];
}


Mar 13 '06 #3

P: n/a
cdg wrote:
Would anyone explain how to return an array that wasn`t passed from
"main" using a pointer. I'm not understanding how scope and memory are
involved in this. void main()
{
int i(0);
int array[20]={0};
Here you declared an array with 20 members, valid indexes 0-19.
array[20] = ArrayFunctionOne();
Then you assigned an int to one past the array. This is undefined
behavior, your program is trash at this point.
int ArrayFunctionOne()
{
int i(0);
int array[20]={0};

for(i=0; i<20; i++)
array[i] = i;

array[20] = ArrayFunctionTwo(array);
And again.
return array[i];


And again.
As Alf said, the best way is to use a std::vector. Unless you have some
overriding reason, that's where a newbie should begin.

Otherwise, you'll have to use some sort of dynamica allocation scheme.
As this is what vector does, there's almost no reason (outside of
homework) for you to do your own handling.

If you feel it is necessary, describe your problem more thoroughly and
we'll see.

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Mar 13 '06 #4

P: n/a
cdg
I was just asking, what is the best way to return an array (not vector)
from a function to a function that is in the program before the array is
declared.
Is this a situaton that requires the "new" statement for dynamically
allocated memory.
Or would declaring a global array before "main" be a better approach.
I am not interested in vectors with this. I am just trying to understand
the problem.
Mar 13 '06 #5

P: n/a
cdg wrote:
I was just asking, what is the best way to return an array (not vector)
from a function to a function that is in the program before the array is
declared.
That's a confusing statement. An array cannot be returned. You can
return a pointer to it's first element.
Is this a situaton that requires the "new" statement for dynamically
allocated memory.
Only if you need to allocate an array.
Or would declaring a global array before "main" be a better approach.
Better than new? Completely different. Globals are rarely better.
I am not interested in vectors with this. I am just trying to understand
the problem.


What exactly are you trying to do?

Looking at the code in your original post, it would seem that you are
returning copies of ints. You probably need to return an int* that
points to the beginning of the array.

The problem is that you do not also return the array length, which makes
it somewhat difficult to do anything with the array.

What is the purpose of your function:

int ArrayFunctionTwo(int []);

It accepts an int*, but you do not tell the function how many elements
there are, so it can't very well do much.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 13 '06 #6

P: n/a
cdg wrote:
I was just asking, what is the best way to return an array (not vector)
from a function to a function that is in the program before the array is
declared.
Is this a situaton that requires the "new" statement for dynamically
allocated memory.
Or would declaring a global array before "main" be a better approach.
I am not interested in vectors with this. I am just trying to understand
the problem.


Neither approach seems very good in general. Unless the array truly
needs to be global, declaring it global is a violation of good scoping
practices. And dynamically allocating it in another function is asking
for trouble once you hand off the responsibility of deleting the array
to some other function.

I'd suggest creating the array in the function that actually needs it
(either dynamically or, if the size is known at compile time, on the
stack) and passing it either by reference or by value (i.e., as a
pointer) to the function that will modify it.
Mar 13 '06 #7

P: n/a
In article <ljjRf.13636$Tf3.4543@dukeread09>,
"cdg" <an****@anywhere.com> wrote:
I was just asking, what is the best way to return an array (not vector)
from a function to a function that is in the program before the array is
declared.
You can't return an array from a function, you return a pointer to an
element in an array (usually the first element.)
Is this a situaton that requires the "new" statement for dynamically
allocated memory.
Or would declaring a global array before "main" be a better approach.
Humm... I expect that for someone new to the language the global would
be less error prone, but if the size of the program grows much bigger
than a page, it wouldn't be better IMHO.
I am not interested in vectors with this. I am just trying to understand
the problem.


The problem is, you can't return an array. Arrays don't really exist in
C++, there are vectors, and there are blocks of memory which you can
dereference with [] so that they look like arrays.

Of the two, vector is more like an array because it supports value
semantics (for eg. you can return one.)

Blocks of memory (like you used in your sample program,) can't be
returned, they just sit in RAM waiting for code to modify them. You can
let functions know where the block of memory is by passing a pointer
into it, but you can't move the block once it is created.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 14 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.