If you hate scopes, why do you artificially create many more of them than you need? Every time you write
you define a new variable of that type, and thereby create a scope. The only places that require a type are declarations: function declarations or variable declarations.
Your problem is that C/C++ allows you to declare variables of the same name multiple times under certain conditions, although the compiler may (and should) issue a warning. However, these variables are all different, and each has it's individual scope! More to the point, each time you put a type in front of a variable name you issue a new declaration and create a new scope. At the same time you hide the previously declared variable(s) of the same name.
1. Do not ignore compiler warnings! If you didn't see any, make sure to set the warning level to the highest value (usually 4), and make a point to investigate each warning and eliminate it.
2. Add the type of a variable only once in your code: in its declaration
3. Use different names for local (or global) variables and function parameters. While it is not mandatory to do so, using the same names is just another source of confusion, and a potential source of errors.
(in this case, main has the local variables
, which coincide with the names you use for the function arguments of
void Names(int i, char &name);
. Note that these variables/arguments define different entities and therefore should use different names!)
4. The type
describes a variable of type reference to character
, i. e. a single character. Your function tries to use it as a char array though. If you wish to pass an array, one way to do it is to pass a pointer to its start, and its length, e. g.:
void Names(char* name_string, size_t name_length)
(note that I used the type
, to describe the length: this type is specifically designed for the use with (array) sizes, or just about anything related to offsets in memory. While you can use
instead, that will likely cause warnings if you try to use it with certain system functions, e. g.
size_t strlen(const char*)
- see item 1. above.)