Array Initialization?

Trying to print out the two dimensional array but how possibly can I. I get an error on int col saying "type int unexpected."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  
//Function Draw Car
void carinitialize ()
{
	char carimage[2][3]={{ '-','-','-'},
					{'|','_','_'}};
	
	int row=0,
	int col=0;
	for (row=0;row<2;row++)
	{
		for (col=0;col<3;col++)
		{
			cout<<carimage[row][col];
		
		}
	
	}

}
either put a semi-colon on line 8 or remove the int on line 9. Also you don't exactly need the second for loop character arrays have a special operator << where you can output the entire array.

1
2
3
4
for( row = 0; row < 2; ++ row )
{
    cout << carimage[row];
}


Also I would suggest using constant variables instead of magic numbers where you have 2 and 3 for rows/columns.
I'll just leave this beautifully whitespaced and indented array initialization here for your pleasure.
1
2
3
4
5
char carImage[ ROWS ][ COLUMNS ] = 
{
	{ '-', '-', '-' },
	{ '|', '_', '_' }
};


You know you could use strings though?
Last edited on

You know you could use strings though?


+1 @ this.

1
2
3
4
5
6
//Function Draw Car
void cardraw () // <- the you are DRAWING a car.. .this name makes much more sense
{
    cout << "---\n"
            "|__";
}
Last edited on
Oh yes, I now that icN use string, but I wanted to try a different method and work on a different approach from something I'm not aware of. In the main function, I would probably declare an array. How can I add parameters to this void function?
And giblit. When you said I can just use carimage[row], does it only work for char arrays?
When you said I can just use carimage[row], does it only work for char arrays?


Yes. And only if the arrays are null terminated... which in your case, they are not, so it would not work for you.
Oh yeah I didn't realize he didn't have them null terminator.
Null terminator from my definition means endl or new line. So if I add a new line it would work?

1
2
3
4
5
6
7
.    
//Function Draw Car
void carinitialize (char carimAge[][3], int size)
{
	char carimage[size][3]={{ '-','-','-'},
					{'|','_','_'}};
	 



My second question is why can't I perform this code?
Last edited on
A null terminator is a null (binary value of 0) character appended to the end of a string to mark the end of the string. Without it... when you pass an array of characters to cout or to any function which takes a C string... it has no way to know where the string data ends.

Adding a newline will not work because a newline is not a null terminator.

Any one of these would work:

1
2
3
4
5
6
7
8
9
10
11
12
char carimage[2][4] = { {'-','-','-',0},
                        {'|','_','_',0} };
                        
// ...or...

char carimage[2][4] = { {'-','-','-','\0'},
                        {'|','_','_','\0'} };
                        
// ...or...

char carimage[2][4] = { "---",    // "double quotes" will automatically
                        "|__" };  //   null terminate the string. 



My second question is why can't I perform this code?


Because 'size' is nonconst and can change depending on how you call the function.
null terminator means that the string ends with a null character '\0' or 0 in ascii. Also what do you mean you can't perform it? I can assume the problem is that you have to dynamically create it AFAIK since those types of arrays the compiler needs to know the size ahead of time so it can reserve the space.

*edit guess I took to long typing disch beat me :P
Last edited on
Thanks for your responses giblet and disch!!! Definitely learning more stuff through you guys. Once again thank you!!! One more question, I have been looking over strings and c-strings for the past few days. My professor told me strings are better then character arrays to use. Is this true, because im finding c-strings more eaiser to use?
He probably means because strings have methods you can use on them and algorithms without having to make them yourself. Other than that they are about the same speed (char array might be maybe a usecond faster).
One more question, I have been looking over strings and c-strings for the past few days. My professor told me strings are better then character arrays to use. Is this true, because im finding c-strings more eaiser to use?


There are pros and cons to each. std::string typically easier to use because:

1) It handles memory management for you (don't have to worry about freeing up dynamically allocated memory)

2) It can hold a string of any size (wheres with a char array, you have a maximum cap before you need to reallocate a larger buffer)

3) There is less risk of overflowing. IE:

1
2
3
4
5
6
7
char bad[] = "foo";  // fine
strcpy( bad, "example" );  // BAD, memory corruption!  Possible program explosion!

// vs.

string good = "foo";  // fine
good = "example";  // also fine 




Basically the benefit to string is that you don't have to worry about how big the string data is... a string will always be able to hold it. You also don't have to worry about allocation and cleanup or bad pointers.

So yeah.. strings are generally safer and easier. I'm a little weirded out that you think char arrays are easier. In fact, you questions in this thread suggest otherwise (ie: not knowing about null terminators, not knowing how/why outputting carimage[rows] would work, etc). There are a lot of subtleties with char arrays that if you get them wrong, things can go very, very bad. But with std::string you usually don't have to worry about them.
Last edited on
Topic archived. No new replies allowed.