简体   繁体   中英

Print a 2D string

complete beginner here. I'm trying to print the elements of a 2d spring like this:

OOO

OOO

OOO

But my program's output looks like this:

OOOOOOOOO

OOOOOO

OOO

Can someone help me please?

int main(void) 
{
  int k;

  char game[3][3] = {
                    "OOO",
                    "OOO",
                    "OOO",
                    }; 

    for (k = 0; k < 3 ; k++) 
{
    printf("%s", game[k]);
    printf("\n");
}

Edit:

I did this and it works now

for (i = 0; i < 3; i++)
  {
    for (j = 0; j < 3; j++)
    {
      printf("%c ", game[i][j]);
    }
    printf("\n");
  }
const char *game[3][3] = { {"O", "O", "O"}, {"O", "O", "O"}, {"O", "O", "O"} };

this is how a 2d array looks like

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM