C - can't use my sizeof implementation as an array size -


i implemented sizeof as recommended. work ok when want print size of variable ,but can't use array size.

this code:

#include <stdio.h> #include <stdlib.h>  #define my_sizeof(var) (size_t)((char *)(&var+1)-(char*)(&var))  int s = 7;  void main() {     int arr[sizeof(s)]; //works ok     int arr2[my_sizeof(s)];//error     printf("%d\n", my_sizeof(s));//works ok     int temp = 0; } 
error   1   error c2057: expected constant expression error   2   error c2466: cannot allocate array of constant size 0 error   3   error c2133: 'arr2' : unknown size   

your implementation my_sizeof not equivalent to sizeof operator in c, compile time operator whereas yours can calculate size @ run time.

so,

int arr[sizeof(s)];  

declares array size sizeof(s) whereas

int arr2[my_sizeof(s)]; 

does the same the array size not calculated @ compile time runtime. work, you'll need support of c99's vlas, compiler doesn't support , errors out.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -