1

The problem is that I cannot see 0 in an array.

I run my program and see 2D array. But instead of 0 (the first element) I see nothing.

Here is the code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    int *Ptr;

    scanf("%d%d", &M, &N); /* Size of array. */

    Ptr = malloc(M*N*sizeof(int));

    for (i = 0; i < M * N; i++) /* Filling in. */
    {
        *(Ptr + i) = i;
    }

    for (i = 0; i < M * N; i++) /* Displaying. */
    {
        if (i % N == 0)
            printf("\n");
        printf("%2.d  ", *(Ptr + i));
    }

    return 0;
}

What is the problem? Is there any way to fix it?

4

2 回答 2

5

The number after the dot is the precision. If the precision is 0 (or does not exist) then printf does not print out 0. In your case you do not need the dot:

printf("%2d ", ...)
于 2013-03-31T01:41:28.973 回答
2

change

printf("%2.d  ", *(Ptr + i));

to

printf("%2d  ", *(Ptr + i));
于 2013-03-31T01:40:57.360 回答