fork download
  1. //http://stackoverflow.com/questions/28524896/casting-pointer-to-memory-buffer-to-pointer-to-vla
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int *getPointer(int num){
  6. return malloc(sizeof(int) * num);
  7. }
  8.  
  9. void test1()
  10. {
  11. #define ARRSIZE 6
  12. int *pointer = getPointer(ARRSIZE);
  13. int (*arrPointer)[ARRSIZE] = (int(*)[ARRSIZE])pointer;
  14. printf("%d\n", sizeof(*arrPointer) / sizeof((*arrPointer)[0]));
  15. }
  16.  
  17. void test2( int arrSize )
  18. {
  19. int *pointer = getPointer(arrSize);
  20. int (*arrPointer)[arrSize] = (int(*)[arrSize])pointer;
  21. printf("%d\n", sizeof(*arrPointer) / sizeof((*arrPointer)[0]));
  22. }
  23.  
  24. int main()
  25. {
  26. test1();
  27. test2( 5 );
  28. test2( 3 );
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0s 2008KB
stdin
Standard input is empty
stdout
6
5
3