0

3.1 - ARRAY arguments, char vs int.
It seems you pass a char OR an int to a substring of an array. Does it matter which one?

ex:

char charValue = 5;
	int intValue = 5;
	string awesomeArray[9] = {"truck", "car", "boat", "table", "ocean", "cat", "board", "finicky"};
	
	//Both of these work. Is either a better choice?
	cout << awesomeArray[charValue];
	cout << awesomeArray[intValue];
	
	system("PAUSE");
	return 0;

Edited by crapgarden: n/a

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Fbody
1

>>Does it matter which one?
No. You can use either, but integers are most common. If you use char then the compiler will have to take the time to promote it to an integer before its used as the index into the array. So you can speed up a program by a few nanoseconds by using ints instead of char.

0

They both work because a char is really just a small int (1-byte instead of typically 4-bytes). The difference is the way the system interprets them by default.

Edited by Fbody: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.