3

I've written code to make a right pyramid out a character.

However, when I execute the program, the last two lines of the pyramid have garbage characters placed after them even when it exceeds the size of the array.

The code is here:

#include <stdio.h>
#include <string.h>

#define ROW 5
int main(void) {
  char array[ROW];
  int x = 0;
  int row = 0;

  for (row = 0; row < ROW; row++) {
    array[x] = 'a';
    if (x < ROW) {
      printf("%s\n", dolla);
    }
    x++;
  }
  getchar();
}

Where are the garbage characters coming from? It's only on lines after the third.

fenceop
  • 1,439
  • 3
  • 18
  • 29
TAC
  • 31
  • 4

3 Answers3

3

The problem in your code is that you have not terminated your string with \0 (null) character. Here's a workout for your code:

#include <stdio.h>
#include <string.h>
#define ROW 5
int main(void)
{
char array[ROW];
int x = 0;
int row = 0;


for(row = 0; row < ROW; row++)
{
array[x] = 'a';

    if(x < ROW)
    {
        array[x+1]='\0';
        printf("%s\n", array);
    }
x++;
}

getchar();
}

I'm no specialist, but I've read the following in many typical C books: int arrays in C are initialized to 0, while char arrays are initialized to garbage.

And yeah, forgot to mention, it's no dolla, it's array.

Pranav Totla
  • 2,182
  • 2
  • 20
  • 28
  • "`char` arrays are initialized to garbage." To add some details to help understand why, memory initialised to "garbage" in C usually just means that the process reserves a block of memory, but doesn't write anything to that block of memory. Whatever was there before, if that piece of memory was already used in the past by the same program or another program, is the "garbage" you get. – Laogeodritt Dec 01 '14 at 08:33
2

char array[ROW+1] = {0}; will help you a lot. You might have assumed array was empty but it was full of random characters. By initializing with {0}, the array starts with all zeroes.

I'm going to assume dolla was a transcription error and that either dolla should be array or that array used to be named dolla.

JS1
  • 4,745
  • 1
  • 14
  • 19
  • @tac @js1 Perhaps of note is that `array` is of size 5 in the question's code, and the loop will write `array[i] = 'a'` for `i` in 0 to 4. This means that on the final iteration of the loop, all 5 elements of the string will be filled and the string will not necessarily be null-terminated at this point (luck of the draw on the memory that comes next). C strings (null-terminated char arrays) should always be at least `max_string_length + 1` in size, to account for the necessary null. – Laogeodritt Dec 01 '14 at 08:38
  • @Laogeodritt In the original code, the string was never null terminated at any point except by luck. With my change I allocated 6 bytes of zeroes so it will be null terminated at all times. I guess I didn't point that out specifically. – JS1 Dec 01 '14 at 08:47
  • Sorry, I missed that you changed the allocation to ROW+1 in your suggested changes. I had only noticed the initialization. – Laogeodritt Dec 01 '14 at 09:03
0

You are probably exceeding the values in the array to make the pyramid which results in printing out garbage values that were there before you even compiled your code. When you print the values, rather than printing out whole array ( which means there can be garbage values included ) you can print up to the point where you have valid values and this can be done by introducing a null character '\0' at the end of the array. Also initializing you array would be a better choice here as then you can debug your code better after seeing the output.

NoorUllah
  • 621
  • 1
  • 5
  • 7