Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

evaluation of expression which is used with sizeof

Is there any expression that would be evaluated as operand of a sizeof. I have come to know in case of variable length operand with sizeof, the expression would be evaluated. But I cant make an example, I wrote the code below,

int a[]={1,2,3};
printf("%d",sizeof(a[1]++));
printf("%d\n",a[1]);

but here I observed from output expression a[1]++ is not evaluating. how to make an example??

like image 840
amin__ Avatar asked Jul 10 '12 19:07

amin__


1 Answers

Your array is not a variable-length array. A variable length array is an array whose size is not a constant expression. For example, data is a variable-length array in the following:

int i = 10;
char data[i];

To see an example of a code that has sizeof evaluate its operand, try something like this:

#include <stdio.h>

int main(void)
{
    int i = 41;
    printf("i: %d\n", i);
    printf("array size: %zu\n", sizeof (char[i++]));
    printf("i now: %d\n", i);
    return 0;
}

It prints:

i: 41
array size: 41
i now: 42
like image 130
Alok Singhal Avatar answered Sep 17 '22 22:09

Alok Singhal