Which are the best manner to return a pointer to a pointer of two-dim array?

Discussion in 'C Programming' started by decker, Nov 8, 2007.

  1. decker

    decker Guest

    People,

    In the below code, I would like to know how I can return correctly the
    two-dim array. Anyone can help me?

    /*************/
    int **ReturnIntPointer(int **array);

    int main(){
    int **vetor;
    **ReturnIntPointer((int **) vetor);
    printf("array[0][0]=%d\n", vetor[0][0])); //for test
    return 0;
    }

    int **ReturnIntPointer(int **array) {

    int i, j;
    int nrows = 4;
    int ncolumns = 4;
    array = malloc(nrows * sizeof(int *));
    for(i = 0; i < nrows; i++)
    { array = malloc(ncolumns * sizeof(int)); }
    for(i = 0; i < nrows; i++)
    {
    for(j = 0; j < ncolumns; j++)
    { array[j] = 1+j; }
    }
    return array;
    }
    /**********************/
    Thanks!
    decker, Nov 8, 2007
    #1
    1. Advertising

  2. decker

    Brian Szmyd Guest

    Re: Which are the best manner to return a pointer to a pointer oftwo-dim array?

    On Thu, 08 Nov 2007 14:50:53 +0000, decker wrote:

    > People,
    >
    > In the below code, I would like to know how I can return correctly the
    > two-dim array. Anyone can help me?
    >
    > /*************/
    > int **ReturnIntPointer(int **array);
    >
    > int main(){
    > int **vetor;
    > **ReturnIntPointer((int **) vetor);
    > printf("array[0][0]=%d\n", vetor[0][0])); //for test
    > return 0;
    > }
    >
    > int **ReturnIntPointer(int **array) {
    >
    > int i, j;
    > int nrows = 4;
    > int ncolumns = 4;
    > array = malloc(nrows * sizeof(int *)); for(i = 0; i < nrows; i++)
    > { array = malloc(ncolumns * sizeof(int)); }
    > for(i = 0; i < nrows; i++)
    > {
    > for(j = 0; j < ncolumns; j++)
    > { array[j] = 1+j; }
    > }
    > return array;
    > }
    > /**********************/
    > Thanks!


    If you are allocating the array inside the function that fills the array,
    why would you need to pass the dumb-dumb point in? It should be
    sufficient to do:

    /* in main() */
    int** vetor = NULL;
    vetor = ReturnIntPointer();

    if (vetor)
    {
    ...
    }


    /* in ReturnIntPointer() */
    int** ret_val = (int**) malloc(nrow*sizeof(int *));
    ....
    return ret_val


    Needless to say you need to free vetor in your main(), otherwise you'll
    have a leak.
    Brian Szmyd, Nov 8, 2007
    #2
    1. Advertising

  3. decker

    loudking Guest

    On Nov 8, 3:50 pm, decker <> wrote:
    > People,
    >
    > In the below code, I would like to know how I can return correctly the
    > two-dim array. Anyone can help me?
    >
    > /*************/
    > int **ReturnIntPointer(int **array);
    >
    > int main(){
    > int **vetor;
    > **ReturnIntPointer((int **) vetor);

    Funny, can your code be compiled?

    I never tried to put a "**" in front of a function call

    .....
    loudking, Nov 8, 2007
    #3
  4. decker

    decker Guest


    > Funny, can your code be compiled?
    >


    Yes, the funny thing that it can be compiled. Strange... Well, I put
    "**" to see how it runs, a curiosity of my part. I will remove so.

    Really I forget to free memory, a basic error. Thanks for the
    reminder!
    decker, Nov 8, 2007
    #4
  5. decker

    Guest

    loudking wrote:
    > On Nov 8, 3:50 pm, decker <> wrote:
    > > People,
    > >
    > > In the below code, I would like to know how I can return correctly the
    > > two-dim array. Anyone can help me?
    > >
    > > /*************/
    > > int **ReturnIntPointer(int **array);
    > >
    > > int main(){
    > > int **vetor;
    > > **ReturnIntPointer((int **) vetor);

    > Funny, can your code be compiled?
    >
    > I never tried to put a "**" in front of a function call


    There's nothing inherently wrong with the **. In context, it's pretty
    pointless, but in a different context it might even be a reasonable
    thing to do. The function returns a pointer to a pointer to int.
    Assuming that both the pointer to the int and the pointer to the
    pointer to int are valid, the return value can be dereferenced twice,
    resulting in the value of the int that was pointed at. This code then
    completely ignores that value, which is why I described it as pretty
    pointless.
    , Nov 8, 2007
    #5
  6. decker

    decker Guest


    > There's nothing inherently wrong with the **. In context, it's pretty
    > pointless, but in a different context it might even be a reasonable
    > thing to do. The function returns a pointer to a pointer to int.
    > Assuming that both the pointer to the int and the pointer to the
    > pointer to int are valid, the return value can be dereferenced twice,
    > resulting in the value of the int that was pointed at. This code then
    > completely ignores that value, which is why I described it as pretty
    > pointless.


    So... you say that the return of the function are incorrect? Which
    manner I can to do to the code don't ignore the values?
    decker, Nov 8, 2007
    #6
  7. decker

    decker Guest


    > There's nothing inherently wrong with the **. In context, it's pretty
    > pointless, but in a different context it might even be a reasonable
    > thing to do. The function returns a pointer to a pointer to int.
    > Assuming that both the pointer to the int and the pointer to the
    > pointer to int are valid, the return value can be dereferenced twice,
    > resulting in the value of the int that was pointed at. This code then
    > completely ignores that value, which is why I described it as pretty
    > pointless.


    So you say that the return of the function is incorrect?
    decker, Nov 8, 2007
    #7
  8. decker

    Guest

    decker wrote:
    > > There's nothing inherently wrong with the **. In context, it's pretty
    > > pointless, but in a different context it might even be a reasonable
    > > thing to do. The function returns a pointer to a pointer to int.
    > > Assuming that both the pointer to the int and the pointer to the
    > > pointer to int are valid, the return value can be dereferenced twice,
    > > resulting in the value of the int that was pointed at. This code then
    > > completely ignores that value, which is why I described it as pretty
    > > pointless.

    >
    > So... you say that the return of the function are incorrect? Which
    > manner I can to do to the code don't ignore the values?


    Your code completely ignores the value of the argument that is passed
    in to ReturnIntPointer(), so there's no reason to pass in that
    argument. Change the appropriate lines as indicated below, and the
    function interface will make a lot more sense. I haven't bothered
    testing your code, so I can't be sure this will fix all of it's
    problems, but it will fix the function interface problem.

    int **ReturnIntPointer(void);

    int main(){
    int **vetor = ReturnIntPointer();
    ....
    }

    int **ReturnIntPointer(void) {
    ...
    int **array = malloc(nrows * sizeof(int *));
    ....
    }
    , Nov 8, 2007
    #8
  9. decker

    Flash Gordon Guest

    Re: Which are the best manner to return a pointer to a pointer oftwo-dim array?

    decker wrote, On 08/11/07 14:50:
    > People,
    >
    > In the below code, I would like to know how I can return correctly the
    > two-dim array. Anyone can help me?
    >
    > /*************/
    > int **ReturnIntPointer(int **array);


    Decide on whether you want to return a pointer to the array or pass in a
    pointer to the variable you want to point to it. Then see the
    comp.lang.c FAQ at http://c-faq.com/ which will answer most of your
    questions.

    > int main(){
    > int **vetor;
    > **ReturnIntPointer((int **) vetor);


    Specifically remember that C passes variables by value, to
    ReturnIntPointer (which is a bad name) cannot change the value of vetor.
    Also the cast is not needed and had you made a mistake would have hidden
    it rather than fixing it.

    > printf("array[0][0]=%d\n", vetor[0][0])); //for test
    > return 0;
    > }
    >
    > int **ReturnIntPointer(int **array) {
    >
    > int i, j;
    > int nrows = 4;
    > int ncolumns = 4;
    > array = malloc(nrows * sizeof(int *));


    Not the best way to use malloc in my opinion. It is easier to maintain
    array = malloc(nrows * sizeof *array);

    You need to check if malloc succeeds.

    > for(i = 0; i < nrows; i++)
    > { array = malloc(ncolumns * sizeof(int)); }


    Similarly here.

    > for(i = 0; i < nrows; i++)
    > {
    > for(j = 0; j < ncolumns; j++)
    > { array[j] = 1+j; }
    > }
    > return array;
    > }

    --
    Flash Gordon
    Flash Gordon, Nov 8, 2007
    #9
  10. On Thu, 08 Nov 2007 14:50:53 -0000, decker <>
    wrote:

    >People,
    >
    >In the below code, I would like to know how I can return correctly the
    >two-dim array. Anyone can help me?
    >
    >/*************/
    >int **ReturnIntPointer(int **array);
    >
    >int main(){
    > int **vetor;
    > **ReturnIntPointer((int **) vetor);


    What was the point of the ** in front of the function name?

    Since vetor is an int**, what is the point of the cast?

    Since vetor was never given a value, its value is indeterminate. The
    function call attempts to evaluate it which invokes undefined
    behavior.

    > printf("array[0][0]=%d\n", vetor[0][0])); //for test


    Since C passes by value, vetor is still indeterminate and this also
    invokes undefined behavior.

    You could avoid both UBs with
    vetor = ReturnIntPointer(NULL);
    though the preferred approach (tm) would be to change the function to
    not have a parameter.

    > return 0;
    >}
    >
    >int **ReturnIntPointer(int **array) {
    >
    > int i, j;
    > int nrows = 4;
    > int ncolumns = 4;
    > array = malloc(nrows * sizeof(int *));
    > for(i = 0; i < nrows; i++)
    > { array = malloc(ncolumns * sizeof(int)); }
    > for(i = 0; i < nrows; i++)
    > {
    > for(j = 0; j < ncolumns; j++)
    > { array[j] = 1+j; }
    > }
    > return array;
    >}
    >/**********************/
    >Thanks!



    Remove del for email
    Barry Schwarz, Nov 10, 2007
    #10
    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. Denis Palas
    Replies:
    1
    Views:
    493
  2. Florian Kaufmann
    Replies:
    1
    Views:
    370
  3. Markus Dehmann
    Replies:
    9
    Views:
    214
    Tad McClellan
    Mar 23, 2005
  4. Andrew Poulos

    Return inex position of value in multi dim array

    Andrew Poulos, Mar 2, 2005, in forum: Javascript
    Replies:
    5
    Views:
    125
  5. Sorting a two dim array

    , Jul 8, 2005, in forum: Javascript
    Replies:
    3
    Views:
    91
Loading...

Share This Page