What's the best way to read multiple numbers/strings in one array/struct/union, which itself is nested in one or more parent arrays/structs/unions?
1st example without temporary variable:
printf("%d %d\n", a[9][3], a[9][4]);
1st example with temporary variable:
int *b = a[9];
printf("%d %d\n", b[3], b[4]);
The temporary variable in the first example above is quite silly I'm sure, but in the second example below it makes sense and looks better to use one, right?
2nd example without temporary variable:
foo[i]->bar.red[j][0]++;
foo[i]->bar.red[j][1]++;
foo[i]->bar.red[j][2]++;
foo[i]->bar.red[j][3]++;
2nd example with temporary variable:
int *p = foo[i]->bar.red[j];
p[0]++;
p[1]++;
p[2]++;
p[3]++;
So where do you draw the line? I realize that compilers are smart enough to insert any indirection needed to produce assembly of optimal efficiency in this way, but (hypothetically assuming extremely performance critical code) maybe there are exceptions? And from a code clarity/maintainability point of view, what's your rule of thumb, if any?
First, I believe that this is purely a matter of code readability/maintainability, that is an individual preference. Compilers are smarter than us today (joke). :-)
Personally I usually think about extracting a temporary variable (or a function, this applies to them as well) in two cases:
Thus, your first example I'd leave as is:
printf("%d %d\n", a[9][3], a[9][4]);
And the second one would be:
int *p = foo[i]->bar.red[j];
p[0]++;
// ...
Or, (often, but not always) better:
int *bar_red = foo[i]->bar.red[j];
bar_red[0]++;
// ...