array pointers

Discussion in 'C++' started by cdg, Mar 13, 2006.

  1. cdg

    cdg Guest

    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;
    }
    cdg, Mar 13, 2006
    #1
    1. Advertising

  2. * 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;
    > }
    >
    >



    --
    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?
    Alf P. Steinbach, Mar 13, 2006
    #2
    1. Advertising

  3. cdg

    Rolf Magnus Guest

    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<<" "<<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;
    > }
    Rolf Magnus, Mar 13, 2006
    #3
  4. cdg

    Default User Guest

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

    --
    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.
    Default User, Mar 13, 2006
    #4
  5. cdg

    cdg Guest

    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.
    cdg, Mar 13, 2006
    #5
  6. cdg

    Ben Pope Guest

    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...
    Ben Pope, Mar 13, 2006
    #6
  7. cdg

    Mark P Guest

    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.
    Mark P, Mar 13, 2006
    #7
  8. cdg

    Daniel T. Guest

    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.
    Daniel T., Mar 14, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter B. Steiger

    Can a static array contain a dynamic array of pointers?

    Peter B. Steiger, Apr 19, 2004, in forum: C Programming
    Replies:
    8
    Views:
    2,080
    Dave Thompson
    Apr 26, 2004
  2. mann!

    pointer to array v/s array of pointers

    mann!, Feb 25, 2005, in forum: C Programming
    Replies:
    6
    Views:
    319
    E. Robert Tisdale
    Feb 26, 2005
  3. Sean
    Replies:
    2
    Views:
    629
    loufoque
    Sep 24, 2006
  4. Piotrek

    pointers and array of pointers

    Piotrek, Apr 2, 2007, in forum: C Programming
    Replies:
    8
    Views:
    321
    Chris Torek
    Apr 6, 2007
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    651
Loading...

Share This Page