0

Return an array from a function

Good afternoon, can anyone explain how to return an array from a function using pointers?

11th Oct 2018, 12:07 AM
Сергей кузичев
Сергей кузичев - avatar
2 Réponses
+ 3
array types are like int[] double[], you can return them like any good ol' datatype.
11th Oct 2018, 2:48 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 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; }
11th Oct 2018, 2:49 AM
non