Queer 2D-Array

This is a discussion on Queer 2D-Array within the C++ Programming forums, part of the General Programming Boards category; I bet you were missing me, I have a problem with my 2D array as it seems to have random ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Queer 2D-Array

    I bet you were missing me,

    I have a problem with my 2D array as it seems to have random values in certain places...

    Code:
    //definition:
    char board[3][3];
    Then when I output certain parts of it to where they should go I get random characters instead of blankness.


  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Provide an initialization if this definition is in block scope, otherwise you are pretty much guaranteed garbage values.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Ok thanks, how exactly do I initialize it to 0?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    char board[3][3] = {0};
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    ah right thanks I wasn't sure how to do it without defining each part.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,487
    As a side note to what Dave already provided you, it is important you understand something...

    Had you defined that array outside any function (and main() is a function), the array would have been initialized for you. Variables of the local type (variables defined inside a function) are not initialized automatically. Variables of the global type, are.

    Also...

    An array can be initialized by providing only a value for the first element, when defining it. That is what Dave provided. But (and this is a big but) never forget that what happens here is that the rest of the array elements are initialized according to the rules of their type, and not to the value you provided. On the case of chars, it means they are initialized to 0.

    Type this...
    Code:
    char board[3][3] = {7, 2};
    And then check your array elements. You will see that, just like before you no longer have garbage values. But you will also realize that the first element of your array contains 7, the second contains 2, and all others contain 0.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Thank you very much, I think you have just solved a problem, cheers.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    How do I clear all the elements of my array after it has been defined?

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,487
    If by clear you mean resetting their values to 0, you can traverse the array elments in a for or while loop and set each element to 0.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21