Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function,returning arrays?

Status
Not open for further replies.

Jastper

Programmer
Oct 4, 2002
13
0
0
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?
 
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;
}


 
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(n);
popArray(ptr,n);

for (x=0 ; x <= n ; x++) {
printf(&quot;%d = %d\n&quot;, 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;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top