holy
holy

Reputation: 632

Function with return type array in C

I have the following function in C:

int[] function(int a){
    int * var = (int*)malloc(sizeof(int)*tags);
    ....
}

*var is it a pointer to an array var?

If yes, how can I return the array (var) in the function?

Upvotes: 11

Views: 45413

Answers (5)

John Bode
John Bode

Reputation: 123578

In C, functions cannot return array types. For your purposes, you want to return a pointer to int:

int *function(int a)
{
  int *var = malloc(sizeof *var * tags); // where is tags defined?
                                         // are you sure you don't mean a here?
  ...
  return var;
}

This will allocate a block of memory large enough to hold tags integer values and assign the address of the first element of that block to var. Note that var is a pointer to int, not a pointer to an array of int. That pointer is what gets returned from the function.

You can use the subscript oprerator on a pointer expression as though it were an array, like so:

int a = ...;
int *arr = function(a);
...
arr[0] = 0;
arr[1] = 1;
...

arr is a pointer expression, not an array expression, so sizeof arr will return the size of the pointer type, not the size of the block of memory that it points to (because of this, you will want to keep track of the number of elements you allocated separately).

Upvotes: 1

Niklas Schnelle
Niklas Schnelle

Reputation: 1249

In C an array is basically the same type as a pointer to an element of the array. So char[] is basically char*

Don't forget to keep track of the size of the array, also I noticed that tags seems to be a global variable, most of the time it's a good idea to avoid global variables

Here is some example code:

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

int* foo(size_t arrSize){
   int* arr = (int*) malloc(sizeof(int)*arrSize);
   return arr;
}


int main (int argc, char** argv){
   printf("Printing array:\n");
   int* arr = foo(42);
   for(int i=0; i <42; i++){
      arr[i]=i;
   }

   for (int i=0; i < 42; i++){
      printf("Element: %d: %d\n", i, arr[i]);
   }
   free(arr);
   return 0;
}

Upvotes: -2

Attila
Attila

Reputation: 28802

How about:

int* function(int tags){ 
  int * var = malloc(sizeof(int)*tags); 
  //.... 
  return var;
} 

Arrays and pointers to the base element type are (mostly) synonymous in C/C++, so you can return a pointer to the first element of an array and use that as if it was the array itself.

Note, your code has an input parameter a, but using tags to allocate the memory for the array. I assumed in the above code that you wanted to use the input parameter for that purpose

Also, you will have to call free() on the pointer returned by function above, when you are no longer using the array, to avoid memory leaks. malloc above allocates memory enough to hold tags number of ints, so the array is equivalent to int var[tags];

UPDATE: removed cast for malloc's return

Upvotes: 1

dAm2K
dAm2K

Reputation: 10349

This code below could clarify a bit how array and pointers works. The function will allocate memory for "tags" int variables, then it will initialize each element with a number and return the memory segment that points to the array. From the main function we will cycle and print the array element, then we will free the no longer needed memory.

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

int *function(unsigned int tags) {
        int i;
        int *var = malloc(sizeof(int)*tags);

        for (i=0; i < tags; i++) {
                var[i] = i;
        }

        return var;
}

int main() {
        int *x;
        int i;

        x = function(10);
        for (i=0; i < 10; i++) {
                printf("TEST: %i\n", x[i]);
        }

        free(x); x=NULL;

        return 0;
}

Upvotes: 2

MByD
MByD

Reputation: 137432

You can't really return an array from a function, but a pointer:

int * function(int a){
    int * var = malloc(sizeof(int)*tags);
    //....
    return var;
}

Upvotes: 20

Related Questions