Dup Ver Goto 📝

Misc_001

PT2/lang/cpp/arrays does not exist
To
27 lines, 95 words, 665 chars Page 'Misc_001' does not exist.

The main point here is to show that a[b] and b[a] are the same thing where one is a pointer and the other is an integer. It also shows random stuff you can do with templates.

#include <stdio.h>

template <typename T, int X=9>
void boing(const char* s) {
  T a[4];
  T* aa = &(a[X]);
  T* aaa = &(X[a]);
  printf("%20s: %lu (%d) -- %p %p\n",s,sizeof(T),X,aa,aaa);
}

int main() {
  boing<char>("char");
  boing<int>("int");
  boing<long,17>("long");
}

Output

                char: 1 (9) -- 0x7fff4592680d 0x7fff4592680d
                 int: 4 (9) -- 0x7fff45926814 0x7fff45926814
                long: 8 (17) -- 0x7fff45926868 0x7fff45926868