fork(1) download
  1. #include <stddef.h>
  2.  
  3. int main(void) {
  4. char a[3][4] = { { 0 } };
  5. size_t sz;
  6. void * ptr;
  7. char c;
  8.  
  9. /*
  10.   * "Array decay"
  11.   *
  12.   * 'a' has type 'char[3][4]', as seen on line 4.
  13.   * 'a[0]' has type 'char[4]'.
  14.   * 'a[0][0]' has type 'char'.
  15.   *
  16.   * But when an expression with an array type appears
  17.   * outside of use as the immediate operand of the
  18.   * 'sizeof' operator or the unary '&' address operator,
  19.   * then the expressions are converted to yield a pointer
  20.   * value that points to the array's element type
  21.   *
  22.   * 'a' goes from 'char[3][4]' to 'char (*)[4]'
  23.   * 'a[0]' goes from 'char[4]' to 'char *'
  24.   */
  25.  
  26. /* Below, 'a' has type 'char[3][4]' */
  27. sz = sizeof a;
  28.  
  29. /* Below, 'a[0]' has type 'char[4]' */
  30. sz = sizeof a[0];
  31.  
  32. /* Below, 'a[0][0]' has type 'char' */
  33. sz = sizeof a[0][0];
  34.  
  35. /* Below, 'a' yields a value with type 'char(*)[4]' */
  36. ptr = a;
  37.  
  38. /* Below, 'a[0]' yields a value with type 'char *' */
  39. ptr = a[0];
  40.  
  41. /* Below, 'a[0][0]' yields a value with type 'char' */
  42. c = a[0][0];
  43.  
  44. /* Below, '&a' yields a value with type 'char(*)[3][4]' */
  45. ptr = &a;
  46.  
  47. /* Below, '&a[0]' yields a value with type 'char(*)[4]' */
  48. ptr = &a[0];
  49.  
  50. /* Below, '&a[0][0]' yields a value with type 'char *' */
  51. ptr = &a[0][0];
  52.  
  53. /* Below, '*a' has type 'char[4]' */
  54. sz = sizeof *a;
  55.  
  56. /* Below, '*a[0]' has type 'char' */
  57. sz = sizeof *a[0];
  58.  
  59. /* Below, '*a' yields a value with type 'char *' */
  60. ptr = *a;
  61.  
  62. /* Below, '*a[0]' yields a value with type 'char' */
  63. c = *a[0];
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0.02s 1716KB
stdin
Standard input is empty
stdout
Standard output is empty