Thread: how to initialize a char matrix with empty elements?

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    19

    how to initialize a char matrix with empty elements?

    hello guys!
    i have a problem (don't know why) and need some help
    I have a matrix se part of the code below
    Code:
    int i, j;
    	char matrix[3][3];
    	for(i=0;i<=9; i++) {
    		if(bo->square[i].piece_type==1) /*bo->square[i].piece_type return an integer */
    			  matrix[i/3][i%3]='+';
    		else if (bo->square[i].piece_type==2) /
    			matrix[i/3][i%3]='*';
    		else matrix[i/3][i%3]=' ';
    		}
    but i get a run time error when i have the last statement matrix[i/3][i%3]=' ';
    (Run-Time Check Failure #2 - Stack around the variable 'matrix' was corrupted.)
    If a remove this statement everything is correct but i get some strange (random character) at the empty matrix element when i print it on the screen
    Can somebody tell me how do i intialize an element of the matrix with an empty character ' ' without geting this error ?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Why the '/' on the end of your 'else if' line? Did you want a '\'? Even still you aren't really continuing the statement on the next line so you don't need it. Take that out and see if it works. That was the only problem I had.
    Last edited by SlyMaelstrom; 08-22-2006 at 05:17 AM.
    Sent from my iPadŽ

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for(i=0;i<=9; i++)
    Also, it should be < 9

    On the last iteration, you access matrix[3][0]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    19
    Quote Originally Posted by Salem
    > for(i=0;i<=9; i++)
    Also, it should be < 9

    On the last iteration, you access matrix[3][0]
    thank you i found the error before i read your message
    but i want to thanks all of you for getting help when i need it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM