Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > array pointers

Reply
Thread Tools

array pointers

 
 
cdg
Guest
Posts: n/a
 
      03-13-2006
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];
}


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-13-2006
* 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?
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      03-13-2006
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];
> }


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      03-13-2006
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.
 
Reply With Quote
 
cdg
Guest
Posts: n/a
 
      03-13-2006
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.


 
Reply With Quote
 
Ben Pope
Guest
Posts: n/a
 
      03-13-2006
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...
 
Reply With Quote
 
Mark P
Guest
Posts: n/a
 
      03-13-2006
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.
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      03-14-2006
In article <ljjRf.13636$Tf3.4543@dukeread09>,
"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.


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
pointers and array of pointers Piotrek C Programming 8 04-06-2007 08:02 AM
Pointers, linked list, array of pointers Sean C++ 2 09-24-2006 01:35 PM
pointer to array v/s array of pointers mann! C Programming 6 02-26-2005 12:59 AM
Can a static array contain a dynamic array of pointers? Peter B. Steiger C Programming 8 04-26-2004 03:07 AM



Advertisments