-1

I've got this tiny program I wrote for a C pointers exercise. I simply take a char array and print it in reverse using a pointer. It works. However, I don't understand why it works. Since I'm starting the for loop at element 9, shouldn't it print 5 undefined elements (usually random junk characters, in my experience) before it gets to the "sdrow"? I feel like I should be filtering the output more.

int main(void)
{
    char sentence[10] = "words";
    char *ptr = sentence;

    for(int i=9;i>=0;i--)
    {
        printf("%c", *(ptr+i));
    }
    puts("");
    return 0;
}

Output:

sdrow
Pen275
  • 21
  • 5
  • 2
    How would you recognise an "undefined element" if you met it in the street? – Kerrek SB Sep 25 '16 at 21:20
  • A few tips: Instead of `printf("%c", c)`, you could use `fputc(c, stdout)`, and instead of `puts("")` you could use `fputc('\n', stdout)`. The replacements are simpler and express more directly that you want to print a character. The first case avoids parsing a format string dynamically when you already know the answer statically. – Kerrek SB Sep 25 '16 at 21:27
  • @Kerrek SB A good compiler will produce the same code using `printf("%c", c);` as well as `fputc(c, stdout);` as well as `puts("");` vs. `fputc('\n', stdout);`. The best one to use is context and style dependent. – chux - Reinstate Monica Sep 26 '16 at 17:03

3 Answers3

2

When you initialize an array and the initializer provides fewer elements than there are in the array, the remaining elements are initialized to zero:

int a[3] = { 1 };   // same as { 1, 0, 0 }

char s[4] = "ab";   // same as { 'a', 'b', 0, 0 }

In other words, your code is printing five null bytes before printing the five letters.

(You can see this easily by redirecting the output to a file and looking at the file size.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • OK, so an auto-initialized 0 in a char array is simply null? I want to clarify this because if I were to print an integer array consisting of, say, the ASCII codes for the above characters it would still print the auto-initialized 0s as well. – Pen275 Sep 27 '16 at 13:15
1

When you initialize an array of fixed size, c will automatically fill any unspecified elements with zeros. C-style strings treat these zeros as the null character, which is also the string termination character. So the first bunch of iterations print a null character, then they start printing actual characters.

0

The rest of the array is initialized by the compiler with zeroes, it's why you see nothing:

int main(void)
{
  char sentence[10] = "words";
  char *ptr = sentence;

  for(int i=9;i>=0;i--)
    {
      printf("%02x", *(ptr+i));
    }
  puts("");
  return 0;
}

However you should not rely on this, since the behavior is undefined. If you were using pointers, you would see something different:

int main(void)
{
  char *sentence = "words";
  char *other ="more";
  char *ptr = sentence;


  for(int i=9;i>=0;i--)
    {
      printf("%c", *(ptr+i));
    }
  puts("");
  return 0;
}
Dominique Lorre
  • 1,168
  • 1
  • 10
  • 19