-5
void inputArray(int* *pa, int *n)
{
    do {
        printf("Input the elements: ");
        scanf_s("%d", n);
        if (*n < 0)
        {
            printf("Error! Input again\n");
        }
    } while (*n < 0);
    *pa = (int*)malloc(*n * sizeof(int));
    for (int i = 0; i < *n; i++)
    {
        printf("Elements a[%d]: ", i);
        scanf_s("%d", pa + i);
    }

}

void outputArray(int* *pa, int n)
{
    printf("\nContent of the array\n");
    for (int i = 0; i < n; i++)
    {
        printf("a[%d] = %d\n", i, *(pa + i));
    }
}

int main()
{
    int *A;
    int n;
    inputArray(&A, &n);
    outputArray(&A, n);
    free(A);
    _getch();
    return 0;
}

When the program display the array, I got the error "Exception thrown at 0x00AD372D (ucrtbased.dll)" I'd tried many times to fix the error, but it still displays the error when the program displays the output of array, please give me some advice. Thanks for reading.

Thomas Vu
  • 105
  • 1
  • 7
  • @closers_and_downvoters: this is far from a good question, but code is acceptable as a [mcve], and the problem is described: program crashes. Comments explaining why this question is so terrible would be welcomed – Serge Ballesta Mar 09 '17 at 11:22
  • @SergeBallesta: Read [ask]. – too honest for this site Mar 09 '17 at 11:28
  • @Olaf: Ok, title may not be the more explicit, and research may have been insufficient. But the code is indeed a [mcve] which many questions fail to provide. So the close reason *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself* does not apply here! Desired behaviour: make the program run, problem: crash, code: present and compilable. BTW, **I** know how to ask... – Serge Ballesta Mar 09 '17 at 11:35
  • @SergeBallesta: - The code is not a MCVE, - no specific problem statement (**where** does it crash? Under which conditions?), but merely a "debug my code" request, - no expected behaviour ("make the program run" - you must be kidding). Just providing some short code is not sufficient. With 50k reps you should know. – too honest for this site Mar 09 '17 at 11:38
  • @Olaf: OP does not know how to use a debugger - if he did he would not have asked the question. So he does not know where the program crashes. Anyway, as the program invokes UB, the input data will have little impact - but I agree here, he should have given an example input. But for a beginner (what I think OP is) I do not expect more. He wants to allocate and input an array in a function, and output it in another one (all the code is related to that single problem). And he cannot understand what happens. As I said, it is far from being a good question, but IMHO the closing reason is unfair. – Serge Ballesta Mar 09 '17 at 12:42
  • @SergeBallesta: Ignorantia legis non excusat. There is enough information how to use a debugger available. It is a basic skill for programmers. Do you also accept answers to be used to comments just because the poster has not enough reps to write a regular comment? Or the typical "gimme teh codez" questions: the posters also migh lack the knowledge to write the code. So they are acceptable? – too honest for this site Mar 09 '17 at 20:10
  • We won't agree on that point. You considere the actual quality of a post, I just wonder whether OP has made an effort and if we can help him to become a better programmer. You want better posts, I want better programmers. Anyway, see you later on so :-) – Serge Ballesta Mar 09 '17 at 21:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137704/discussion-between-serge-ballesta-and-olaf). – Serge Ballesta Mar 09 '17 at 21:37
  • @Olaf Thank you for pointing at which I didn't do is that learning how to use debug. Just because I am a newbie in coding and Stackoverflow so I didn't follow the asking question rules, sorry if I made you angry. Have a nice day – Thomas Vu Mar 10 '17 at 10:37

2 Answers2

1

You have a double pointer int* *pa in you function inputArray, of wich you allocated space for *pa. and you are reading value to pa. First you should allocate memory for pa then read value to it.

 for (int i = 0; i < *n; i++)
{
    pa[i] = malloc(sizeof(int));
    printf("Elements a[%d]: ", i);
    scanf_s("%d", pa[i]);
}
1

A debugger will have shown you where the problems are.

You pass the address of a pointer to inputArray and get it as an int **: fine

You allocate the array with *pa = (int*)malloc(*n * sizeof(int)); : almost fine. All indirections levels are correct, but you should not cast malloc in C (should be: *pa = malloc(*n * sizeof(int));)

But scanf_s("%d", pa + i); is plain wrong. pa is a pointer that contains the address of the allocated array, so the array is at *pa not at pa. You should write scanf_s("%d", *pa + i);

For outputArray, you have no reason to pass a pointer to the array. But if you do, the values will be at (*pa)[i], not at *(pa + i) which is the same as pa[i], so you should use : printf("a[%d] = %d\n", i, (*pa)[i]);.

But the correct way would be:

void outputArray(int *pa, int n)
{
    printf("\nContent of the array\n");
    for (int i = 0; i < n; i++)
    {
        printf("a[%d] = %d\n", i, pa[i]);
    }
}
...
outputArray(A, n);
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • but I have a question, I tried to change from *pa + i to *(pa + i) there are the different value. Can you explain to me about that ? – Thomas Vu Mar 09 '17 at 12:56
  • @VũNguyễnHuyHoàng It is a question of operator priority and grouping. `*pa + 1` is `(*pa) + i`, not `*(pa + i)`. Think of `2+3*4` (14) because it is in fact `2+(3*4)` and not `(2+3)*4` (would be 20) – Serge Ballesta Mar 09 '17 at 13:03
  • I am so sorry about my lack of knowledge of coding, I've just learned it for weeks, so if I didn't know how to put a question on here, be pleasure with me. Have a nice day – Thomas Vu Mar 10 '17 at 10:33
  • Thank you, I've just got it. – Thomas Vu Mar 10 '17 at 10:44