Advertisement
RicardasSim

initialize an array

May 14th, 2019
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.64 KB | None | 0 0
  1. //-std=c99 -Wall -Wextra -Wpedantic -Wshadow
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. typedef struct Testas {
  8.    int i;
  9.    char c;
  10. } Testas;
  11.  
  12. int main (int argc, char **argv)
  13. {
  14.  
  15. unsigned int uiTmp = 3;
  16.  
  17. //={0}; error: variable-sized object may not be initialized
  18. Testas tst[uiTmp];
  19.  
  20. //memset( tst, 0, sizeof(Testas)*uiTmp);
  21. memset( tst, 0, sizeof(tst));
  22.  
  23. printf("size of Testas: %zu\n",sizeof(Testas));
  24. printf("size of tst: %zu\n",sizeof(tst));
  25.  
  26. for(unsigned int i = 0; i < uiTmp; ++i)
  27. {
  28.    printf("%d %d\n", tst[i].i, tst[i].c);
  29. }
  30.  
  31. return 0;
  32. }
  33.  
  34. /*
  35. size of Testas: 8
  36. size of tst: 24
  37. 0 0
  38. 0 0
  39. 0 0
  40. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement