Re: а кто C хорошо знает?
08.10.2012 15:33, Dmitrii Kashin пишет:
> --------------- trick.c ---------------
> 1
> 2 #include <stdio.h>
> 3
> 4 struct base
> 5 {
> 6 int count;
> 7 int str[0];
> 8 };
> 9
> 10 int main(int argc, char** argv)
> 11 {
> 12 int c;
> 13 printf("Input length> ");
> 14 scanf("%d",&c);
> 15
> 16 union
> 17 {
> 18 struct base body;
> 19 int str[sizeof(struct base)+c];
> 20 } object;
> 21
> 22 object.body.count = c;
> 23
> 24 for (int i=0; i<object.body.count; i++)
> 25 {
> 26 object.body.str[i] = c - i;
> 27 }
> 28
> 29 for (int i=0; i<object.body.count; i++)
> 30 {
> 31 printf("object.body.str[%d] = %d\n",i,object.body.str[i]);
> 32 }
> 33
> 34 printf("sizeof(struct base) = %d\n",(int)sizeof(struct base));
> 35 printf("sizeof(int) = %d\n",(int)sizeof(int));
> 36
> 37 return 0;
> 38 }
> 39
> ---------------------------------------
>
> Компиляция и работа:
>
> ---------------------------------------
> % c99 trick.c -o trick
> % ./trick
> Input length> 3
> object.body.str[0] = 3
> object.body.str[1] = 2
> object.body.str[2] = 1
> sizeof(struct base) = 4
> sizeof(int) = 4
> %
> ---------------------------------------
>
> Замечу, что в книге Кернигана и Ритчи "Язык программирования Си"
> подобных фишек не описывалось. Виктор, Вы не подскажете, где можно
> почитать о подобных трюках?
В зависимости от того, где вы хотите применять трюки:
http://govnokod.ru/
http://underhanded.xcott.com/
http://ru.wikipedia.org/wiki/International_Obfuscated_C_Code_Contest
А также неплохо почитать про "принцип наименьшей неожиданности".
Reply to: