Re: array semantic query

2009-07-18 Thread dharmendra pandit
Hi,

I tried the following simple code segment in gcc and it gave the
incompatible type error as mentioned below.

int main() {
    int arr[10];
    arr = arr;   // error: incompatible types when assigning to type
‘int[10]’ from type ‘int *’
}

Here it seems GCC is retaining the left hand side type of arr to be
array of 10 ints whereas on the right hand side
it has changed its type from array to pointer to integer. I tried
searching the relevant sections in the standard ISO C
document number WG14/N1124 justifying the above behaviour of GCC but
failed to conclude it from the specifications.
It would be of great help if someone can point me out the relevant
sections from the specs.

Thanks
Dharmendra


array semantic query

2009-07-18 Thread dharmendra pandit
Hi,

I tried the following simple code segment in gcc and it gave the
incompatible type error as mentioned below.

int main() {
int arr[10];
arr = arr; // error: incompatible types when
 // assigning to type ‘int[10]’ from type ‘int *’
}

Here it seems GCC is retaining the left hand side type of arr to be
array of 10 ints whereas on the right hand side
it has changed its type from array to pointer to integer. I tried
searching the relevant sections in the standard ISO C
document number WG14/N1124 justifying the above behaviour of GCC but
failed to conclude it from the specifications.
It would be of great help if someone can point me out the relevant
sections from the specs.

Thanks
Dharmendra


Re: array semantic query

2009-07-18 Thread dharmendra pandit
According to 6.3.2.1 Para 3, the type conversion from "array of type"
to "pointer to type" should
happen irrespective of whether the operand is on right had side or the
left hand side of assignment
operator. But GCC is converting only the right side operator type to
"pointer of type" while retaining the
left hand side type to be "array of type".

-Dharmendra

On Sat, Jul 18, 2009 at 3:51 PM, Andrew Haley wrote:
> On 07/18/2009 10:35 AM, dharmendra pandit wrote:
>> Hi,
>>
>> I tried the following simple code segment in gcc and it gave the
>> incompatible type error as mentioned below.
>>
>> int main() {
>>     int arr[10];
>>     arr = arr;   // error: incompatible types when assigning to type
>> ‘int[10]’ from type ‘int *’
>> }
>>
>> Here it seems GCC is retaining the left hand side type of arr to be
>> array of 10 ints whereas on the right hand side
>> it has changed its type from array to pointer to integer. I tried
>> searching the relevant sections in the standard ISO C
>> document number WG14/N1124 justifying the above behaviour of GCC but
>> failed to conclude it from the specifications.
>> It would be of great help if someone can point me out the relevant
>> sections from the specs.
>
> 6.3.2.1 Para 3.
>
> I'm surprised you ask, since this convention has been used since early
> K&R C.
>
> Andrew.
>
>


Re: Pre and post increment

2009-07-20 Thread dharmendra pandit
As the C specification document specifies in section 6.5.2.2 point no 10:

The order of evaluation of the function designator, the actual arguments, and
subexpressions within the actual arguments is unspecified, but there
is a sequence point
before the actual call.

Therefore if in any function call if two or more arguments modify same
variable using some expression
then the order in which the expressions will be evaluated are
unspecified, therefore gcc warning that
operation on 'a' may be undefined. Also there is a sequence point
before the actual call of the function
so all the argument expressions must finish evaluation before the
actual function call.

Also C specification document gives the following example explaining
the above behaviour.

12   EXAMPLEIn the function call
  (*pf[f1()]) (f2(), f3() + f4())
  the functions f1, f2, f3, and f4 may be called in any order. All
side effects have to be completed before
  the function pointed to by pf[f1()] is called.

-Dharmendra

On Mon, Jul 20, 2009 at 9:00 PM,  wrote:
> Hello,
>
> Here is a program with output in gcc (4.3.2) on pre and post increments:
>
> //code begin
> #include 
>
> main () {
>    int a;
>    a=1; printf ("1. %d %d\n", ++a, a);                     // 1. 2 2
>    a=1; printf ("2. %d %d\n", a, a++);                     // 2. 2 1
>    a=1; printf ("3. %d %d\n", a++, a);                     // 3. 1 2
>    a=1; printf ("4. %d %d\n", a++, ++a);                   // 4. 2 3
>    a=1; printf ("5. %d %d\n", ++a, a++);                   // 5. 3 1
>    a=1; printf ("6. %d %d %d\n", ++a, a, a++);             // 6. 3 3 1
>    a=1; printf ("7. %d %d %d\n", a++, a, ++a);             // 7. 2 3 3
>    a=1; printf ("8. %d %d %d %d\n", a, a++, ++a, a);       // 8. 3 2 3 3
>    a=1; printf ("9. %d %d %d %d\n", a, ++a, a++, a);       // 9. 3 3 1 3
>    a=1; printf ("10. %d %d %d %d %d\n", a, a++, a, ++a, a);// 10. 3 2 3 3 3
>    a=1; printf ("11. %d %d %d %d %d\n", a, ++a, a, a++, a);// 11. 3 3 3 1 3
> }
> //code end
>
> The output from the program is listed next to it in comments. I thought
> I knew something about pre and post increments, but this program busted
> my understanding of the pre and post increments. I would appreciate
> if someone could explain me the output.
>
> Thanks
>
>
>