Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I would hope a practicing programmer would realize that sizeof is a keyword and evaluated at compile time, and use that. I don't consider this article to be an example of something that you should consider putting in your codebase; but an investigation into some of the language's rules.


> I would hope a practicing programmer would realize that sizeof is a keyword and evaluated at compile time

I must nitpick. sizeof may or may not be evaluated at compile time. It is not possible to always evaluate it at compile time (see VLAs). The standard even includes an example of this:

         #include <stddef.h>
         size_t fsize3(int n)
         {
               char b[n+3];                  // variable length array
               return sizeof b;              // execution time sizeof
         }

          int main()
          {
                size_t size;
                size = fsize3(10); // fsize3 returns 13
                return 0;
          }


You are correct. I forgot about variable length arrays.


well, the title ("how to find size of an array in C without sizeof") certainly made it sound as though there was some use to this!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: