if I am right you want something like that :
#define VARIABLE_LENGTH_CHAR_ARRAY(name, size) \
const int size_of_##name = size; \
char name[size_of_##name]
int main()
{
VARIABLE_LENGTH_CHAR_ARRAY(local_char_array, 16);
}
The name of the (now const) variable for the size now depends on the name of the array itself, that minimize the probability to have homonyms
The expansion of that code produced by gcc -E gives :
int main()
{
const int size_of_local_char_array = 16; char local_char_array[size_of_local_char_array];
}
But to do that it is strange :
- as
__J__ I think this not helps to make the program readable
- else where in your source
size_of_local_char_array can be used but if you/someone search for its definition it will not be found
- the macro produces two statements, and of course in that case it is not possible to group them in a block
{}, this is dangerous because this is not intuitive. As you can see in your code you added a useless ';' after the use of the macro while a final ';' is already present in the macro definition
#name[temp_array_size_macro_index];- This is going to be the string literal"name[temp_array_size_macro_index];". I don't think you meant that.char local_char_array[16];is a lot more understandable thanVARIABLE_LENGTH_CHAR_ARRAY(local_char_array, 16);