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?

5
  • It seems that 0 exists (the first element of array is '0', the 2-nd element = '1' and so on...) Commented Mar 31, 2013 at 1:38
  • The dot in the printf format string, remove it. Commented Mar 31, 2013 at 1:39
  • Thnx! This is very helpful comment. But why don't you "Answer" the quiestion? If you did so, I would be able to "Accept" it. Commented Mar 31, 2013 at 1:43
  • If you're going to give us an SSCCE, pay attention to all of the criteria. This codes isn't compilable... Commented Mar 31, 2013 at 1:52
  • >>> This codes isn't compilable.. ME: I don't understand why.. >>> If you're going to give us an SSCCE ME: I'm not trying to do something else beside of trying to solve the problem. And I cannot understand whether it is good or bad remark for me. Commented Mar 31, 2013 at 1:58

2 Answers 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 ", ...) 
Sign up to request clarification or add additional context in comments.

Comments

2

change

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

to

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.