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.
This is advantageous because the caller can allocate that memory wherever they want.