declaring a function that returns a pointer to 1-d array

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Sep 17, 2005.

  1. Guest

    How can I declare a function that returns a pointer
    to one dimensional array ?
    , Sep 17, 2005
    #1
    1. Advertising

  2. Emmanuel Delahaye, Sep 17, 2005
    #2
    1. Advertising

  3. Emmanuel Delahaye, Sep 17, 2005
    #3
  4. wrote:
    > How can I declare a function that returns a pointer
    > to one dimensional array ?
    >

    Like you declare any other funtion, just remember that the scope of
    local variables are restricted to within the function. So you have to
    return a pointer to dynamically allocated memory, or to a static variable.

    cheers,
    forayer

    --
    If you would be a real seeker after truth, it is necessary that at least
    once in your life you doubt, as far as possible, all things."
    -- Rene Descartes
    Vimal Aravindashan, Sep 17, 2005
    #4
  5. writes:
    > How can I declare a function that returns a pointer
    > to one dimensional array ?


    Do you really want a pointer to an array, or do you want a pointer to
    its first element (which you can then use to access all the elements)?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 17, 2005
    #5
  6. Guest

    Keith Thompson wrote:
    > writes:
    > > How can I declare a function that returns a pointer
    > > to one dimensional array ?

    >
    > Do you really want a pointer to an array, or do you want a pointer to
    > its first element (which you can then use to access all the elements)?
    >

    I want a pointer to an array. (not the pointer to the first element).
    , Sep 17, 2005
    #6
  7. pete Guest

    wrote:
    >
    > Keith Thompson wrote:
    > > writes:
    > > > How can I declare a function that returns a pointer
    > > > to one dimensional array ?

    > >
    > > Do you really want a pointer to an array,
    > > or do you want a pointer to
    > > its first element
    > > (which you can then use to access all the elements)?
    > >

    > I want a pointer to an array. (not the pointer to the first element).


    /* BEGIN new.c */

    #include <stdio.h>

    int (*f(void))[11];

    int main(void)
    {
    printf("f()[0][2] is %d.\n", f()[0][2]);
    putchar('\n');
    return 0;
    }

    int (*f(void))[11]
    {
    static int a[11] = {1, 3, 5, 7};

    return &a;
    }

    /* END new.c */


    --
    pete
    pete, Sep 17, 2005
    #7
  8. Emmanuel Delahaye, Sep 17, 2005
    #8
  9. writes:
    > Keith Thompson wrote:
    >> writes:
    >> > How can I declare a function that returns a pointer
    >> > to one dimensional array ?

    >>
    >> Do you really want a pointer to an array, or do you want a pointer to
    >> its first element (which you can then use to access all the elements)?
    >>

    > I want a pointer to an array. (not the pointer to the first element).


    Ok. Why?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 17, 2005
    #9
  10. Guest

    pete wrote:
    > wrote:
    > >
    > > Keith Thompson wrote:
    > > > writes:
    > > > > How can I declare a function that returns a pointer
    > > > > to one dimensional array ?
    > > >
    > > > Do you really want a pointer to an array,
    > > > or do you want a pointer to
    > > > its first element
    > > > (which you can then use to access all the elements)?
    > > >

    > > I want a pointer to an array. (not the pointer to the first element).

    >
    > /* BEGIN new.c */
    >
    > #include <stdio.h>
    >
    > int (*f(void))[11];
    >
    > int main(void)
    > {
    > printf("f()[0][2] is %d.\n", f()[0][2]);
    > putchar('\n');
    > return 0;
    > }
    >
    > int (*f(void))[11]
    > {
    > static int a[11] = {1, 3, 5, 7};
    >
    > return &a;
    > }
    >
    > /* END new.c */
    >
    >


    Thanx a lot ...
    , Sep 17, 2005
    #10
  11. Guest

    Keith Thompson wrote:
    > writes:
    > > Keith Thompson wrote:
    > >> writes:
    > >> > How can I declare a function that returns a pointer
    > >> > to one dimensional array ?
    > >>
    > >> Do you really want a pointer to an array, or do you want a pointer to
    > >> its first element (which you can then use to access all the elements)?
    > >>

    > > I want a pointer to an array. (not the pointer to the first element).

    >
    > Ok. Why?
    >


    There's no specific reason for this. I am learning C and this came to
    my mind but I was not able to figure it out. Anyway thanx to pete
    for the answer.

    I also want to know how can we return a pointer to first element
    of a 2-d array ? what would be the declaration for such a function
    (the function that returns the pointer to the first element of
    a 2d array whose elements are of type int) ?
    For eg. suppose I have a 2d array
    int arr[3][4];
    I want to return the address of first element of arr.
    Can I do
    return(arr);
    But then what would be the decalaration of such a function (I mean the
    return type ?) will it be "int *" or "int **" ?

    Thanx in advance for any help ...
    , Sep 17, 2005
    #11
  12. wrote:
    > Keith Thompson wrote:
    > [snip]
    >>Ok. Why?
    >>

    >
    >
    > There's no specific reason for this. I am learning C and this came to
    > my mind but I was not able to figure it out. Anyway thanx to pete
    > for the answer.
    >
    > I also want to know how can we return a pointer to first element
    > of a 2-d array ? what would be the declaration for such a function
    > (the function that returns the pointer to the first element of
    > a 2d array whose elements are of type int) ?
    > For eg. suppose I have a 2d array
    > int arr[3][4];
    > I want to return the address of first element of arr.
    > Can I do
    > return(arr);
    > But then what would be the decalaration of such a function (I mean the
    > return type ?) will it be "int *" or "int **" ?
    >
    > Thanx in advance for any help ...
    >

    The return type will be "int **" but returning 'arr' when 'arr' is
    declared as arr[3][4] won't do. Read the first couple of replies to your
    post. And when asking new questions, please do so in a separate thread.

    cheers,
    forayer

    --
    If you would be a real seeker after truth, it is necessary that at least
    once in your life you doubt, as far as possible, all things."
    -- Rene Descartes
    Vimal Aravindashan, Sep 17, 2005
    #12
  13. writes:
    [...]
    > I also want to know how can we return a pointer to first element
    > of a 2-d array ? what would be the declaration for such a function
    > (the function that returns the pointer to the first element of
    > a 2d array whose elements are of type int) ?
    > For eg. suppose I have a 2d array
    > int arr[3][4];
    > I want to return the address of first element of arr.
    > Can I do
    > return(arr);
    > But then what would be the decalaration of such a function (I mean the
    > return type ?) will it be "int *" or "int **" ?


    What exactly do you mean by the "first element" of a 2d array?

    Strictly speaking, C doesn't have 2-dimensional arrays; it has arrays
    of arrays. Given
    int arr[3][4];
    the first element of arr is an array, not an int.

    A two-dimensional array can also be implemented as an array of
    pointers, or as a pointer-to-pointer, rather than as an array of
    arrays. This is more flexible, since it allows you to vary the size
    of the subarrays (but you have to keep track of it).

    Confusingly, given:

    int x[10][10];
    int (*y)[10];
    int **z;

    and assuming memory has been allocated properly, all of the following
    are valid expressions of type int:

    a[0][0]
    b[0][0]
    c[0][0]

    but they all have different meanings.

    If you want to return a pointer to its first element, use

    return &arr[0];

    The type is pointer-to-array-of-3-int

    If you want to return a pointer to the first element of the first
    element, which is of type int, use:

    return &arr[0][0];

    Both of these can be transformed if you like, since x[y] is equivalent
    to *(x+y).

    But in general, pointers to arrays are rarely useful. The pointed-to
    array type must be of fixed size, the syntax is confusing, and
    whatever you're trying to do can usually be done more easily with a
    pointer to the first element, rather than a pointer to the entire
    array.

    Another thing: given "int arr[3][4];", the type int** isn't going to
    be useful. int** is a pointer to a pointer; the array declaration
    doesn't create any pointer objects for an int** to point to.

    Arrays are not pointers. Pointers are not arrays. C's syntax almost
    seems designed to fool you into forgetting this.

    If you haven't read section 6 of the C FAQ (Arrays and Pointer), read
    it. If you have, read it again.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 17, 2005
    #13
    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. Brandon
    Replies:
    2
    Views:
    479
    Arne Adams
    Aug 8, 2004
  2. lou zion
    Replies:
    7
    Views:
    6,827
    Rolf Magnus
    Feb 28, 2005
  3. Replies:
    12
    Views:
    831
    Dave Thompson
    Sep 4, 2006
  4. Replies:
    2
    Views:
    380
  5. pembed2012
    Replies:
    1
    Views:
    413
    James Kuyper
    Feb 27, 2012
Loading...

Share This Page