Can I return an array from a function?

Returning an array as a whole is not possible in c-language. However you may use one of the following method:

1. Store an array within a structure and then return the structure.
2. Dynamically allocate space for the array and then return the pointer
to that allocated space.

Comments
Always to the point :)

Returning an array as a whole is not possible in c-language. However you may use one of the following method:

1. Store an array within a structure and then return the structure.
2. Dynamically allocate space for the array and then return the pointer
to that allocated space.

Can give some example?

Edited 7 Years Ago by hket89: n/a

you can also make a global variable and edit its contents in your function, so you wont be in need of returning anything


to dynamically allocate you can:

#include <stdio.h>

char *function (void)
{
    char *myArray = "Hello World!\n";
    return myArray;
}

int main (void)
{
    char *arr;  
    arr = function ();
    printf ("arr:%s\n", arr);
    getchar();
}

Edited 7 Years Ago by Alibeg: more info

you can also make a global variable and edit its contents in your function, so you wont be in need of returning anything


to dynamically allocate you can:

#include <stdio.h>

char *function (void)
{
    char *myArray = "Hello World!\n";
    return myArray;
}

int main (void)
{
    char *arr;  
    arr = function ();
    printf ("arr:%s\n", arr);
    getchar();
}

Thanks for your help...

there is a way to make a pointer and then edit the content of what that pointer points to using a function:

void func (char *p)
{
p[index_of_element] = 'A';
.....
}

int main(void)
{
char *ptr;
ptr =(char *) malloc (size_of_an_element * number_of_elements);
func(ptr);
// content of what ptr points to is now changed and can be used normally
...
}

that would be using a dynamic allocation and last post wasnt quite dynamical allocation but creating an array in function and then saving it using return.

Cheers.

Edited 7 Years Ago by Alibeg: mised a row of code

Yes, that is easily possible. Just use a pointer to a pointer.

Basically, in the same way a pointer can "point" to a piece of memory that holds some data (like an int or a char), a pointer can "point" to another pointer. Basically, you pass a pointer to a pointer as an argument to the function, and you dereference that crazy thing to get a plain old pointer. The cool thing is that it *will* change the original pointer. (This craziness is required for the same reason you can't just modify non-array data passed as parameters.)

Here's an example of a function that would allocate memory for a char pointer.

#include <stdlib.h>

void alloc_char(char **cp)
{
    *cp = (char *)malloc(sizeof(char));
}

int main(int argc, char *argv[])
{
    char *my_char;
    /* We pass the address of the pointer so that we can modify the original copy! */
    alloc_char(&my_char); // &my_char is a char **
    /* Don't forget to free! */
    free(my_char);
    return 0;
}

I hope that helps!

This question has already been answered. Start a new discussion instead.