RicardasSim

extern array

Jan 21st, 2020
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. /*
  2. -std=c99 -Wall -Wextra -Wpedantic -Wshadow
  3. */
  4.  
  5. /*
  6.  test_inc.h
  7. ------------
  8. */
  9.  
  10. #ifndef TEST_INC_H
  11. #define TEST_INC_H
  12.  
  13. #define ARR_1_SIZE 10
  14. #define ARR_2_SIZE 20
  15.  
  16. //error: invalid application of ‘sizeof’ to incomplete type ‘int[]’
  17. //extern int arr_1[];
  18.  
  19. extern int arr_1[ARR_1_SIZE];
  20. extern int arr_2[ARR_2_SIZE];
  21.  
  22. int testFnc();
  23.  
  24. #endif
  25.  
  26. /*
  27.  main.c
  28. --------
  29. */
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include "test_inc.h"
  34.  
  35. int arr_1[ARR_1_SIZE];
  36. int arr_2[ARR_2_SIZE];
  37.  
  38. int main()
  39. {
  40.  
  41.     printf("1: %zu\n", sizeof arr_1  / sizeof *arr_1 );
  42.     printf("2: %zu\n", sizeof arr_2  / sizeof *arr_2 );
  43.  
  44.     testFnc();
  45.  
  46.     return 0;
  47. }
  48.  
  49. /*
  50.  file1.c
  51. ---------
  52. */
  53.  
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include "test_inc.h"
  57.  
  58. int testFnc()
  59. {
  60.  
  61.     printf("1a: %zu\n", sizeof arr_1  / sizeof *arr_1 );
  62.     printf("2a: %zu\n", sizeof arr_2  / sizeof *arr_2 );
  63.  
  64.     return 0;
  65. }
  66.  
  67. /*
  68. output:
  69.  
  70. 1: 10
  71. 2: 20
  72. 1a: 10
  73. 2a: 20
  74.  
  75. */
Advertisement
Add Comment
Please, Sign In to add comment