Note: if you pass an array in, like this,
Code:
int zerofirst(int *array) {
array[0] = 0;
}
then it might do as you expect: the first element in the array is initialized to zero, and this change is reflected in the calling function. You see, you don't make a copy of the array when you pass it to the function; that would be quite expensive. Instead, you just get a pointer to the first element of the array, which is still located in main()'s (or whatever) stack memory. Thus if you modify the array, you actually modify the original.
Last note: you need some space for the array, as I mentioned below. The syntax is easy if you know how much space you need.
Code:
int main() {
int array1[10], array2[100];
/* ... */
return 0;
}
---
The rest of my reply assumes you were trying to create new arrays and return them to the caller, which is somewhat more difficult.
Almost. You need to write into some valid memory. Also, the syntax in function() would be
since you want to dereference array1 to get to the actual array, and then index into that array.
You might want something like this, e.g.
Code:
#include <stdio.h>
#include <stdlib.h>
void new_number_list(int **upwards, int **downwards, int length) {
int n;
*upwards = malloc(sizeof(int) * length);
*downwards = malloc(sizeof(int) * length);
for(n = 0; n < length; n ++) {
(*upwards)[n] = n;
(*downwards)[n] = length - n - 1;
}
}
int main() {
int *up, *down, x;
new_number_list(&up, &down, 10);
printf("Up: ");
for(x = 0; x < 10; x ++) {
printf("%d ", up[x]);
}
printf("\nDown: ");
for(x = 0; x < 10; x ++) {
printf("%d ", down[x]);
}
putchar('\n');
free(up);
free(down);
return 0;
}
Note: if you're only creating one array like this, you can just return it:
Code:
int *new_array(int length) {
return malloc(length * sizeof(int));
}