0

I am trying to initialize a 2D array to create a map that will print out stars at the beginning of the program. I have my initialization in a function. Whenever I try to run the program, I get crazy numbers. Any tips on how to make this 2D array correct? This is my code and the result that I get:

void InitializeArray()
{
char map[Y_DIM][X_DIM];

for (int row = 0; row < Y_DIM; row++)
{
   for (int col = 0; col < X_DIM; col++)
   {
      cout << map[row][col];
      cout << "*";
   }
cout << endl;
}
}

This is my result

`*2*.*v*/***************
******************
?*?*?*u*/****?*?*?*?*?*?*?*?*?*!*`**
****?*!*`******?*!*`******
?*-*?**?****U*?*7*v*/****?**@**
****?*?*7*v*/*****E*]*v*/****
?*!*`*******-*?**?****?*!*`**
****?**@******p*    *@******
?*-*?**?****************
****?*?*7*v*/****p* *@******
1
  • 'I am trying to initialize a 2D array'. I don't see your initialization. Commented Apr 3, 2014 at 0:43

2 Answers 2

3

That is not initializing the array, it's printing it. Given that it's not initialized, it prints garbage.

Instead of:

 cout << map[row][col];

you want:

map[row][col] = '*';

That will set the initial value for each cell in your array, which is to say, initialize it.

You can also do this at the same time you define the array using C++'s array initializer syntax, but your approach is better.

Sign up to request clarification or add additional context in comments.

2 Comments

I need to initialize and then print it. How would I initialize?
@savannaalexis 'How would I initialize?' He just wrote this in his answer: map[row][col] = '*';!!
0

What I'd do:

#include <iostream>
using namespace std;
const int Y_DIM = 8;
const int X_DIM = 9;
void initializeArray() {
    char map[Y_DIM][X_DIM]={'*'};
    for (int row = 0; row < Y_DIM; row++)
    {
        for (int col = 0; col < X_DIM; col++)
        {
            map[row][col]='*';
            cout << map[row][col];
        }
    cout << "\n";   
}
}
int main() {
    initializeArray();
    return 0;
}

Output

*********
*********
*********
*********
*********
*********
*********
*********

Try it on ideone.com

5 Comments

={'*'}; is redundant.
@πάνταῥεῖ Just making sure it get's allocated. Once I made string str; and then got some garbage when I tried to see what it contained. Then changed to string str = ""; and all was back to normal :)
In this case, it will cause the first element of map to be initialized to '*' and all subsequent elements to get 0. You then overwrite those values in the loop, so πάντα ῥεῖ is right: it's redundant.
@David, if that string story is true then you had some other memory corruption in your program that happened to be overflowing and reading the memory of your string
@MattMcNabb It is true, I've been initializing all my variables since then. D:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.