Nov 18, 2002 #1 Jastper Programmer Oct 4, 2002 13 GB I am having diffuculty passing an array back to main from a function. Can anyone show me how to call and return two values from in this way?
I am having diffuculty passing an array back to main from a function. Can anyone show me how to call and return two values from in this way?
Nov 18, 2002 #2 jmnagi Programmer Oct 17, 2002 47 GB is this what you mean ? #include <stdio.h> #include <stdlib.h> #include <string.h> void reverse (char local_arr[], int index, int len) { int j; for (j=len;j>0;j--) { ... ... } } int main (int argc, char *argv[]) { ... ... char arr[10][20]; ... memset(arr,'\0',20); reverse(&arr[n][0],i,k); ... return 0; } Upvote 0 Downvote
is this what you mean ? #include <stdio.h> #include <stdlib.h> #include <string.h> void reverse (char local_arr[], int index, int len) { int j; for (j=len;j>0;j--) { ... ... } } int main (int argc, char *argv[]) { ... ... char arr[10][20]; ... memset(arr,'\0',20); reverse(&arr[n][0],i,k); ... return 0; }
Nov 18, 2002 #3 marsd IS-IT--Management Apr 25, 2001 2,218 US Simpler example, without static allocation. This is a more generic method IMO. #include <stdio.h> #include <stdlib.h> int *allocArray(int len); void popArray(int *arr,int l); int main(void) { int n = 50, x; int *ptr; ptr = allocArray; popArray(ptr,n); for (x=0 ; x <= n ; x++) { printf("%d = %d\n", x, ptr[x]); } free(ptr); return 0; } int *allocArray(int len) { int *myarray = malloc(len * sizeof(int)); if (myarray) { return myarray; } return NULL; } void popArray(int *arr,int l) { int y = 0; while (y++ <= l) { arr[y] = 1 + rand() % 6; } } Upvote 0 Downvote
Simpler example, without static allocation. This is a more generic method IMO. #include <stdio.h> #include <stdlib.h> int *allocArray(int len); void popArray(int *arr,int l); int main(void) { int n = 50, x; int *ptr; ptr = allocArray; popArray(ptr,n); for (x=0 ; x <= n ; x++) { printf("%d = %d\n", x, ptr[x]); } free(ptr); return 0; } int *allocArray(int len) { int *myarray = malloc(len * sizeof(int)); if (myarray) { return myarray; } return NULL; } void popArray(int *arr,int l) { int y = 0; while (y++ <= l) { arr[y] = 1 + rand() % 6; } }