array pointers

C

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<<" "<<endl;

}

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

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

array[20] = ArrayFunctionTwo(array);

return array;

}

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

Alf P. Steinbach

* 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<<" "<<endl;

}

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

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

array[20] = ArrayFunctionTwo(array);

return array;

}

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

Rolf Magnus

cdg said:
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<<" "<<endl;

}

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

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

array[20] = ArrayFunctionTwo(array);

return array;

}

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

Default User

cdg said:
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;

array[20] = ArrayFunctionTwo(array);


And again.
return array;


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
 
C

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.
 
B

Ben Pope

cdg said:
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
 
M

Mark P

cdg said:
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.
 
D

Daniel T.

"cdg said:
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.
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top