fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. // create two arrays we care about
  7. const char *ages = "\x17\x2b\x0c\x59\x02";
  8. const char (*names)[6] = (void *)
  9. "Alan\0\0" "Frank\0" "Mary\0\0" "John\0\0" "Lisa\0\0";
  10.  
  11. // safely get the size of ages
  12. int count = strlen(ages);
  13. int i = 0;
  14.  
  15. // first way using indexing
  16. for(i = 0; i < count; i++) {
  17. printf("%s has %d years alive.\n",
  18. *(names+i), *(ages+i));
  19. }
  20.  
  21. printf("---\n");
  22.  
  23. // setup the pointers to the start of the arrays
  24. const char *cur_age = ages;
  25. const char (*cur_name)[6] = names;
  26.  
  27. // second way using pointers
  28. for(i = 0; i < count; i++) {
  29. printf("%s is %d years old.\n",
  30. *(cur_name+i), *(cur_age+i));
  31. }
  32.  
  33. printf("---\n");
  34.  
  35. // third way, pointers are just arrays
  36. for(i = 0; i < count; i++) {
  37. printf("%s is %d years old again.\n",
  38. *(names+i), *(ages+i));
  39. }
  40.  
  41. printf("---\n");
  42.  
  43. // fourth way with pointers in a stupid complex way
  44. for(cur_name = names, cur_age = ages;
  45. (cur_age - ages) < count;
  46. cur_name++, cur_age++)
  47. {
  48. printf("%s lived %d years so far.\n",
  49. *cur_name, *cur_age);
  50. }
  51.  
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 0s 1832KB
stdin
Standard input is empty
stdout
Alan has 23 years alive.
Frank has 43 years alive.
Mary has 12 years alive.
John has 89 years alive.
Lisa has 2 years alive.
---
Alan is 23 years old.
Frank is 43 years old.
Mary is 12 years old.
John is 89 years old.
Lisa is 2 years old.
---
Alan is 23 years old again.
Frank is 43 years old again.
Mary is 12 years old again.
John is 89 years old again.
Lisa is 2 years old again.
---
Alan lived 23 years so far.
Frank lived 43 years so far.
Mary lived 12 years so far.
John lived 89 years so far.
Lisa lived 2 years so far.