473,405 Members | 2,160 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,405 software developers and data experts.

array pointers

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
7 3672
* 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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: dam_fool_2003 | last post by:
Friends, cannot we malloc a array? So, I tried the following code: int main(void) { unsigned int y={1,3,6},i,j; for(i=0;i<3;i++) printf("before =%d\n",y); *y = 7; /* 1*/
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
19
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
2
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.