I need to scan n numbers and then fill an array with the following scanned n numbers. I have to use both n and array inputs in a function as well as to use dynamic memory.
void input(int* buff, int* n);
int main() {
int* data = NULL;
int n;
input(data, &n);
for (int i = 0; i < n; ++i) {
printf("%d ", data[i]);
}
return 0;
}
void input(int* buff, int *n) {
if (scanf("%d", n) == 1 && getchar() == '\n') {
if (*n <= 0) {
error = 1;
}
}
if (error == 0) {
buff = (int*)calloc(*n, sizeof(int));
if (buff == NULL) {
error = 1;
}
}
if (error == 0) {
int count = 0;
int p;
for (int i = 0; i < *n; i++) {
if (1 == scanf("%d", &p)) {
if (count < *n) {
*(buff + count) = p;
count++;
}
}
}
for (int i = 0; i < *n; i++) {
printf("%d, ", *(buff + i));
}
}
I didn't insert all the logic of my program in the first for since it doesn't really mater.
The last for prints proper elements however I have a warning about an empty pointer.
When I return to the main and trying to print I've got an error about read access prohibition.
Where did I do wrong?