2

I have this pointer to pointer function that takes two arguments and it is suppose to return pointer to the array. But what is the proper way to return pointer to the array in this case? Here is my code:

#include <stdio.h>
#include <stdlib.h>

int **multiTable (unsigned int xs, unsigned int ys)
{
    unsigned int i, j;

    int **table = malloc( ys * sizeof(*table)); 
          // should I put malloc(ys * xs * sizeof(*table))?

    for(i = 0; i < ys; i++)
    {
        for(j = 0; j < xs; j++)
        {
           table[i][j] = (j+1) * (i+1);
        }

    }

    return table;  //what would be the proper way to return table? Is this ok?

    free(**table); //by the way, do I need this? 
}

int main()
{
    int sum = 0;
    sum = multiTable2(3,2);

    return 0;
}

what would be the proper way to return table? Is what I written the return table; ok or should it be like return *table or even return **table? And also, do I need to free the table?

8
  • 1
    You allocate table, but you don't allocate table[x], leading to *undefined behavior, which you also have in that free call. Commented Dec 13, 2014 at 11:18
  • Then your assignment in the main function is wrong as well. All in all, your compiler should be screaming at you! Commented Dec 13, 2014 at 11:18
  • And your free call is really wrong, very much. Does this code compile? Commented Dec 13, 2014 at 11:19
  • Besides you try to free after return. Commented Dec 13, 2014 at 11:20
  • 1
    Yes, it does compline. So should I change the malloc to int (*table)[xs] = malloc(ys * sizeof(*table)); Commented Dec 13, 2014 at 11:24

2 Answers 2

1

I think this is what you are trying to do.

#include <stdio.h>
#include <stdlib.h>

int **multiTable(unsigned int xs, unsigned int ys)
{

    unsigned int i, j;
    int **table;

    table = malloc(ys * sizeof(int *)); /* you need space for ys integer arrays */
    if (table == NULL)
        return NULL;
    for (i = 0 ; i < ys ; i++)
    {
        table[i] = malloc(xs * sizeof(int)); /* you need space for xs integers */
        if (table[i] != NULL) /* did malloc succeed? */
        {
            for (j = 0 ; j < xs ; j++)
                table[i][j] = (j + 1) * (i + 1);
        }
    }
    return table;
}

int main()
{
    int **sum;

    sum = multiTable(3, 2);
    /* do somenthing with sum here */
    if (sum != NULL)
    {
        /* now you must use free, like this */
        unsigned int i;
        for (i = 0 ; i < 2 ; i++)
        {
            if (sum[i] != NULL)
                free(sum[i]);
        }
        free(sum);
    }
    return 0;
}

this could be done, but you need to read more to understand the basics.

Your problem is not

how to return a pointer

but instead how to use malloc and free.

Sign up to request clarification or add additional context in comments.

4 Comments

If I would like to use only one malloc, would this work: int (*table)[xs] = malloc(ys * sizeof(*table))
Or int **table = malloc(ys * xs * sizeof(*table))?
no, it would be something like int *table = malloc(xs * ys * sizeof(int)) but then you would need to compute the index your self.
I mean to get the i,j element you do it this way table[i * columnCount + j].
0

If I would like to use only one malloc, would this work: int (*table)[xs] = malloc(ys * sizeof(*table))

Yes, it would, if you adjust the function according to Matt McNabb's comment:

#include <stdio.h>
#include <stdlib.h>

int (*multiTable(unsigned int xs, unsigned int ys))[]
{
    unsigned int i, j;
    int (*table)[xs] = malloc(ys * sizeof *table);
    for (i = 0; i < ys; i++)
    for (j = 0; j < xs; j++)
       table[i][j] = (j+1) * (i+1);
    return table;
}

int main()
{
    const int xs = 3, ys = 2;
    int (*sum)[xs];
    sum = multiTable(xs, ys);
    int i, j;
    for (i = 0; i < ys; i++, puts(""))
    for (j = 0; j < xs; j++)
       printf("%d", sum[i][j]);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.