Advertisement
SepandMeenu

Array size in C

Aug 27th, 2020 (edited)
1,381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. /*
  2.  find the size of a char array in two ways
  3. */
  4.  
  5. #include <stdio.h>
  6.  
  7. unsigned int strsize(char* str) {
  8.   unsigned int s = sizeof(str) / sizeof(str[0]);
  9.   return s;
  10. }
  11.  
  12. int main(void) {
  13.   char str [5] = "abcde";
  14.   // inline
  15.   int size1 = sizeof(str) / sizeof(str[0]);
  16.   // function call
  17.   int size2 = strsize(str);
  18.  
  19.   printf("* size: expected  %d\n", 5);
  20.   printf("  inline)         %d\n", size1);
  21.   printf("  function call)  %d\n", size2);
  22.   return 0;
  23. }
  24.  
  25. // compile>>> gcc -o strsize.out strsize.c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement