Nic Nic - 1 year ago 87
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:


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


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 Source

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?