Nic Nic - 2 months ago 7
C Question

Printing first index in array prints every index with C

I am starting to learn C and have a question about arrays.

Here I have a 2-dimensional simple array of characters:

char memory[100][6];
...
// populate indexes


and here I try to print out the first row:

puts(memory[0]);


Here is the output. Why does every row print out?

00P00300P10404P1ZZZZ0000ZZ0010


I come from Java where if you run the same code you will only get the first row. Am I missing something or is this just how C handles arrays? If so, how can I just get the first row?

Answer

The puts function is strictly for C-style strings. If you pass it a pointer to something other than a C-style string, garbage is likely to result. How are you expecting it to know how many characters to output?