-2

I want to return a dynamic array by reference from a void function. I already searching 3 hours for the answer, couldn't find anything helpfull. Here is my simplified code :

main()
{
    int **a;

    xxx(&a);

    printf("%d\n\n", a[1]);

}

void xxx(int **a)
{
    int i;

    *a = (int*)malloc(5 * 4);

    for (i = 0; i < 5; i++)
        a[i] = i;
    printf("%d\n\n", a[1]);
}

I just want to allocate dynamic array in the "xxx" Function and return it by reference to main, than I want to print it or use it for something else. Thanks in advance :)

edit

 #include <stdio.h>
 #include <stdlib.h>
 #define MACROs
 #define _CRT_SECURE_NO_WARNINGS

 void xxx(int **a);


 int main(void)
 {
   int *a;

   xxx(&a);

   printf("%d\n\n", a[1]);
 }


 void xxx(int **a)
 {
   int i;

   *a = malloc(5 * sizeof(**a));

   for (i = 0; i < 5; i++)
        a[i] = i;
   printf("%d\n\n", a[1]);
 }
Jen
  • 3
  • 4
  • [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Oct 08 '16 at 19:20
  • You are close, but there are still *many* things wrong with the code. You should really listen to your compiler as it will yell warnings at you. And if not then you need to turn up the warning level (which you should do anyway). – Some programmer dude Oct 08 '16 at 19:21
  • Don't use "magic numbers". Either `#define` them (for array length say), or use `sizeof` (for standard or more complex user defined types). – Weather Vane Oct 08 '16 at 19:22
  • That is very old code: obsolete `main` definition and no function prototype. Are you using a circa 1990 compiler? Modern C compilers are available, and free. And you do not use `#include` files. – Weather Vane Oct 08 '16 at 19:30
  • Any reason you don't just **return** the pointer? What use does it have to make a functions `void` although you want to **return** something? K.I.S.S! Only use pointers to storage if you already return something else. – too honest for this site Oct 08 '16 at 20:37
  • @Olaf that is true but since the array size is hard coded, the array need not even be dynamic, perhaps in `main` just `int a[5];`. But the OP wants to know how to do this in a function with pointers. – Weather Vane Oct 08 '16 at 21:32
  • I am studying computer science and this was part of a question in a test. I had to create an array in the function and put there some data. Than I had to return the size and the array. Because obviously we can return only one value, I had to return the size with the "return" command and the array by reference. – Jen Oct 08 '16 at 21:54
  • @Olaf Just want to add something, the reason I didn't create the array in main is because I don't know the size. The size only known in the second function after I copy some data from a linked list by a condition. That's why we had to create it in the second function. – Jen Oct 08 '16 at 22:13

3 Answers3

1

I have amended a few things and added some comments.

#include <stdio.h>                      // please inlcude relevant headers
#include <stdlib.h>

#define ELEM 5                          // you can change the requirement with a single edit.

void xxx(int **a)                       // defined before called - otherwise declare a prototype
{
    int i;
    *a = malloc(ELEM * sizeof(int));    // do not use magic numbers, don't cast
    if(*a == NULL) {
        exit(1);                        // check memory allocation
    }
    for (i = 0; i < ELEM; i++) {
        (*a)[i] = i;                    // index correctly
    }
}

int main(void)                          // 21st century definition
{
    int *a;                             // correct to single *
    int i;
    xxx(&a);
    for (i = 0; i < ELEM; i++) {        // show results afterwards
        printf("%d ", a[i]);
    }
    printf("\n");
    free(a);                            // for completeness
}

Program output:

0 1 2 3 4
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

In your main(), you need to have a pointer, not a pointer to pointer. change

  int **a;

to

 int *a;

and, inside the xxx(), change

 a[i] = i;

to

(*a)[i] = i;

That said

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
-1

Ok guys so the thing that made it work is

a[i] = i;

to

(*a)[i] = i;

3 hours for such a simple answer. Thank you very much everybody here. Can some explain why it was the problem?

Jen
  • 3
  • 4
  • Well, it was not the only problem, another was `int **a;` in `main`. But `(*a)[i] = i;` introduces the extra level of indirection necessary - and re the first point in this comment, removed that indirection in `main`. – Weather Vane Oct 08 '16 at 20:14
  • @WeatherVane Before I posted here I tried to change things in the code like from **a to *a but nothing worked, I tried many thing but didn't even thought about doing this (*a)[i] = i. Honestly I didn't understand why this should be like that but thanks anyway – Jen Oct 08 '16 at 22:15