Returning Arrays/Pointers from a function

passing and returning arrays in c
how to return an array from a function in c++
return pointer to array c++
return dynamically allocated array c
how to return arrays
how to return a character array from a function in c++
return array from function geeksforgeeks
how to return array of pointers from function in c

I am trying to create a new integer array which is derived from a string of characters. For example :

char x[] = "12334 23845 32084";  

int y[] = { 12334, 23845, 32084 };

I am having trouble understanding how to return an array ( which I understand isn't possible ) from a function.

I originally tried :

/* Convert string of integers into int array. */
int * splitString( char string[], int n )
{
    int newArray[n];

    // CODE

    return ( newArray );
}

int main( void )
{
    int x[n] = splitString( string, n );

    return ( 0 );
}

I later learned that you can not do this.

How do pointers work in regards to functions?

Thank you.

Typically, you require the caller to pass in the result array.

void splitString( const char string[], int result[], int n) {
    //....
}

This is advantageous because the caller can allocate that memory wherever they want.

Returning Arrays/Pointers from a function, If you don't want to get in trouble learning malloc and dynamic memory allocation you can try this #include <stdio.h> void initArray(int n,� Dynamically allocating the commandArray as suggested by others is insufficient as the allocated array being returned contains pointers into the input array and the input array is also on the stack so goes out of scope when the function getCommand() returns.

The problem is you're returning a pointer to something on the stack. You need to create your array on the heap, then free it when you're done:

int * splitString( char string[], int n )
{
    int *newArray = malloc(sizeof(int) * n);

    // CODE

    return ( newArray );
}

int main( void )
{
    int *x = splitString( string, n );

    // use it

    free(x);

    return ( 0 );
}

How to return an array from a function with pointers, Return array from function in C. C programming does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index. struct function_table my_table [] = { {"function1", function1}, So you can reach into the table by name and call the "associated" function. Or maybe you use a hash table in which you put the function and call it "by name". Regards Friedrich

int * splitString( char string[], int n )
{
    int newArray[n];
    return ( newArray );
}

This is very bad! The array newArray local to the function gets destroyed when the function returns. You'd be left out with a dangling pointer and using it would invoke undefined behaviour.

You can't return an array from a function. The best you can do is

int * splitString( char string[], int n )
{
    int *newArray = malloc(n*sizeof(int)); // the array gets allocated on the heap rather than on the stack(1)
    // Code 
    return ( newArray );
}

Don't forget to free the allocated memory.

(1) Note that the standard doesn't use/define the term stack or heap as such.

Return array from function in C, Declare “a function with argument of int* which returns pointer to an array of 4 integer pointers”. At the first glance it may look complex, we can declare the� How it works: Since the name of an array is a pointer to the 0th element of the array. Here we are passing two arguments to the function return_pointer().The arr is passed using call by reference (notice that name of the array is not preceded by & operator because the name of the array is a constant pointer to the 0th element of the 1-D array) and i is passed using call by value.

Rather than returning an array with return (newArray), you return a pointer to the first element of newArray.

The problem is that you're allocating the array the wrong way. If you instantiate it with int newArray[n], memory gets allocated on the current stack frame. That memory will be freed as soon as your function returns, and whatever was in the array will be garbage. Instead, do the following:

int *newArray = malloc(n * sizeof(int));
// etc.
return newArray

By using malloc, you allocate memory on the heap, where it will survive past the end of the current stack frame. Just remember to free(newArray) somewhere in your program when you're done.

Declare a C/C++ function returning pointer to array of integer , How To Return An Array From Function In C And C++?. CppNuts play list for smart Duration: 8:28 Posted: Jan 9, 2018 The above search function can be used for any data type by writing a separate customized compare(). 7) Many object oriented features in C++ are implemented using function pointers in C. For example virtual functions. Class methods are another example implemented using function pointers. Refer this book for more details.

You can wrap an array in a structure and then return an instance of the structure. I'm mentioning this for completeness, it's not really something you'd want to do as it's ugly and there are better alternatives.

#include <stdio.h>

struct retval
{
    int a[10];
};

struct retval test()
{
    struct retval v = {{1, 5, 6}};
    return v;
}

int main()
{
    struct retval data = test();
    printf("%d %d\n", data.a[1], data.a[2]);
}

How To Return An Array From Function In C And C++?, static int Z = 0; int *pointer_to_Z (int x) { /* function returning integer pointer, not pointer to function */ return &Z; } int� function returning pointer vs function returning array in C Tag: c , arrays , pointers func1 is giving warning & junk value while func2 is giving the right output.

C Programming/Pointers and arrays, Since the name of an array is a pointer to the 0th element of the array. Here we are passing two arguments to the function return_pointer() . The arr is passed� When returning a pointer from a function, do not return a pointer that points to a value that is local to the function or that is a pointer to a function argument. Pointers to local variables become invalid when the function exits.

Returning a Pointer from a Function in C, Functions Pointers Example; Functions with Array Parameters; Functions that Return an Array; Function Pointers; Array of Function Pointers� – Pointer arrays • Pointers as function arguments • In a function, if a pointer argument is de- the function is returning the memory

Functions Pointers in C Programming with Examples, You can pass a pointer to the first element of an array the caller controls to the function and have the function fill that array with data. This is the most common� (For now, arrays, pointers, types and records will be covered in a later chapter) Functions/Procedures . Before we begin, let us first clarify the key difference between functions and procedures. A procedure is set of instructions to be executed, with no return value. A function is a procedure with a return value. For readers familiar with C

Comments
  • This is the preferred method. In 99% of the cases it doesn't make sense to return any type of pointer from a function.
  • Right. As mentioned in other answers, you could use malloc(n*sizeof(int)) in you function to allocate the memory and return that pointer, but then you always have to remember to eventually free the memory as well. Since the malloc is hidden in a function at a different level of abstraction, you're prone to forget. So it's best to allocate the memory and pass the pointer in to the function, use the result, and then free the memory so you can easily match up your mallocs with your frees.
  • @JCooper: The caller can also allocate that memory from a pool, off the stack, or wherever else he wants to put it, but the function cannot.
  • your syntax is not correct. The [] come after the name of the parameter.
  • Yes I realise that now. I have scoured the web for some examples on how to implement correctly however I can not find anything.
  • @prasoonSaurav , so you have mentioned , newArray local to the function gets destroyed so we return pointer. Pointer usually stores address of the memory, then if we return pointer that contains address of the memory we created within the function, then that memory will also be destroyed Right? Could you please explain me, I couldn't find the answer
  • Just wondering, how does this work? isn't "v.a" a pointer to a location on the stack of "test"? Does "struct retval data = test();" result in a deep copy of the array?
  • You don't need a cast