0
Return an array from a function
Good afternoon, can anyone explain how to return an array from a function using pointers?
2 Answers
+ 3
array types are like int[] double[], you can return them like any good ol' datatype.
+ 1
In C:
char*
strfunc (void) {
char *ret = malloc(100);
if ( 0 > sprintf(ret, "Hello World\n\0") ) {
free(ret);
ret = NULL;
}
return ret;
}
int
main (int argc, char **argv) {
char *arr = strfunc();
if (arr == NULL)
return -1;
int ii = strlen(arr) -1;
printf("Array:\n");
while (ii > -1)
printf("%c", arr[ii]);
printf("\n");
free(arr);
return 0;
}