0

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?

2 Answers 2

3

data in main isn't changed when you change buff inside input. In other words - data will still be NULL when the input function returns.

It's just like the n... For n you correctly pass a pointer to n and you need to do exactly the same for data.

Like:

void input(int** buff, int *n) {
              ^^
           // Notice the two *, i.e. pointer-to-pointer
    ....
    *buff = calloc(....
    ....

and call it like

input(&data, &n);
Sign up to request clarification or add additional context in comments.

9 Comments

And if i need to assign a scanned value to a buff inside this function, should I use that? *buff[count] = p
@Arzental In your current code (after the change I suggested), you replace all buff with (*buff)
Thanks, it helped. I've found a[2] == *(a + 2) == *(2 + a) == 2[a] So in my case it is *(*buff + count) == *buff[count] ? However right part doesn't work. Unlike your correction (left).
@Arzental Check C operator precedence... Ask yourself: Is *buff[count] the same as *(buff[count]) or the same as (*buff)[count]. That's important.... In other words... when I told you to replace buff with (*buff) there was a reason for (....)
@Arzental I strongly recommend: int n; --> int n = 0;
|
0

If you only need to allocate one single dynamic array in your function you can just return a pointer to it:

int *input(int *buf, int *n);
...
    data = input(data, &n);
...
int *input(int* buff, int *n) {
    ...
    return buff;
}

2 Comments

Nitpick: If input returns a pointer to the allocated memory, there is no need for buff as a function parameter.
@4386427: You are right, except that is allows to use an existing (allocated) array or increase it when required with realloc. But I should have said it in my answer. A bit too lazy today...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.