By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Initializing array problem

blackstormdragon
P: 32
I just started working with arrays and was trying my hand at a two-dementional array. I dont understand why it keeps telling me I have to many initializers.

char board[3][3]=
{
"-","-","-",
"-","-","-",
"-","-","-"
};

[3][3] Means 3 rows and 3 columns and thats what I've initialized. All 9 places should contain the char - .

Thank you.
Mar 10 '07 #1
Share this Question
Share on Google+
7 Replies


P: 3
I just started working with arrays and was trying my hand at a two-dementional array. I dont understand why it keeps telling me I have to many initializers.

char board[3][3]=
{
"-","-","-",
"-","-","-",
"-","-","-"
};

[3][3] Means 3 rows and 3 columns and thats what I've initialized. All 9 places should contain the char - .

Thank you.
Except that you haven't used characters to initialise the array, you've used null-terminated character string constants (each one contains _two_ characters - the value you've set and a terminating null). P.S. Why aren't you using vectors?
Mar 10 '07 #2

blackstormdragon
P: 32
Except that you haven't used characters to initialise the array, you've used null-terminated character string constants (each one contains _two_ characters - the value you've set and a terminating null). P.S. Why aren't you using vectors?
Vectors???
Mar 10 '07 #3

blackstormdragon
P: 32
We haven't learned about vectors yet. We just started arrays. Even if I knew how to use vectors, this program must be made using arrays.
Mar 10 '07 #4

P: 3
We haven't learned about vectors yet. We just started arrays. Even if I knew how to use vectors, this program must be made using arrays.
Ah, following a course .... apologies.

Just replace the double quotes with single quotes and you should be OK.

For the record '-' is of type char (either signed or unsigned depending on your implementation) whereas "-" is of type const char * (i.e. a C-style string constant). That's why your compiler is complaining.
Mar 10 '07 #5

Ganon11
Expert 2.5K+
P: 3,652
You are also initializing your array incorrectly. The data in {'-', '-', '-', '-', '-', '-', '-', '-', '-'} is an array of 9 characters, but a 3x3 array is actually an array (with three elements) or character arrays (each with 3 elements). So you have to initialize the array of arrays weith arrays, and each of those arrays is initialized with an array of characters.

All that means is you change this:

Expand|Select|Wrap|Line Numbers
  1. char arr[3][3] = {'-', '-', '-', '-', '-', '-', '-', '-', '-'};
to this:

Expand|Select|Wrap|Line Numbers
  1. char arr[3][3] = { {'-', '-', '-'}, {'-', '-', '-'}, {'-', '-', '-'} };
Mar 10 '07 #6

blackstormdragon
P: 32
Thanks so much both of you. Everything is working now. And jbannon don't worry you didnt know I was in a class. I proably should have mentioned it in the first place. Once again thank you both very much.
Mar 10 '07 #7

P: 3
Thanks so much both of you. Everything is working now. And jbannon don't worry you didnt know I was in a class. I proably should have mentioned it in the first place. Once again thank you both very much.
No problem. Being a learner myself I know the position you're in. The more modern books by the likes of Francis Glassboro & Andrew Koenig introduce vectors & iterators before they introduce arrays. For the record, vector is the C++ standard language for "dynamic array" of some type. It behaves a bit like an array but has a lot of additional functionality (and a few quirks) that you'll come across later in your course no doubt.
Mar 10 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.