public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* array semantic query
@ 2009-07-18  9:38 dharmendra pandit
  0 siblings, 0 replies; 5+ messages in thread
From: dharmendra pandit @ 2009-07-18  9:38 UTC (permalink / raw)
  To: gcc

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: array semantic query
  2009-07-18  9:35 ` dharmendra pandit
       [not found]   ` <4A61A230.9000703@redhat.com>
@ 2009-07-18 12:31   ` Zoltán Kócsi
  1 sibling, 0 replies; 5+ messages in thread
From: Zoltán Kócsi @ 2009-07-18 12:31 UTC (permalink / raw)
  To: gcc

> 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

And rightly so.

> 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.

The C99 spec (I only have the draft one, but I think it's pretty
much the same as the final) says, in 6.2.2.3:

 Except when it is the operand of the sizeof operator or the unary &
 operator, or is a character string literal used to initialize an array
 of character type, or is a wide string literal used to initialize an
 array with element type compatible with wchar_t, an lvalue that has
 type ‘‘array of type ’’ is converted to an expression that has type
 ‘‘pointer to type ’’ that points to the initial element of the array
 object and is not an lvalue. If the array object has register storage
 class, the behavior is undefined.

That was spelled out (with different words) in the old K&R C and hasn't
changed since. You can't assign arrays. Since ANSI C you can assign,
pass and return structures and unions, but the array semantics did not
change.

Regards,

Zoltan

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: array semantic query
  2009-07-18 12:18     ` dharmendra pandit
@ 2009-07-18 12:26       ` Richard Guenther
  0 siblings, 0 replies; 5+ messages in thread
From: Richard Guenther @ 2009-07-18 12:26 UTC (permalink / raw)
  To: dharmendra pandit; +Cc: Andrew Haley, gcc, gcc-help

On Sat, Jul 18, 2009 at 2:18 PM, dharmendra
pandit<pandit.dharmendra@gmail.com> wrote:
> 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".

An array is never a valid lvalue in C.  The error is slightly misleading.

Richard.

> -Dharmendra
>
> On Sat, Jul 18, 2009 at 3:51 PM, Andrew Haley<aph@redhat.com> 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.
>>
>>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: array semantic query
       [not found]   ` <4A61A230.9000703@redhat.com>
@ 2009-07-18 12:18     ` dharmendra pandit
  2009-07-18 12:26       ` Richard Guenther
  0 siblings, 1 reply; 5+ messages in thread
From: dharmendra pandit @ 2009-07-18 12:18 UTC (permalink / raw)
  To: Andrew Haley; +Cc: gcc, gcc-help

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<aph@redhat.com> 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.
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: array semantic query
       [not found] <6215dcff0907180232l1c7291eclbc006cabd02224a4@mail.gmail.com>
@ 2009-07-18  9:35 ` dharmendra pandit
       [not found]   ` <4A61A230.9000703@redhat.com>
  2009-07-18 12:31   ` Zoltán Kócsi
  0 siblings, 2 replies; 5+ messages in thread
From: dharmendra pandit @ 2009-07-18  9:35 UTC (permalink / raw)
  To: gcc

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-07-18 12:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-18  9:38 array semantic query dharmendra pandit
     [not found] <6215dcff0907180232l1c7291eclbc006cabd02224a4@mail.gmail.com>
2009-07-18  9:35 ` dharmendra pandit
     [not found]   ` <4A61A230.9000703@redhat.com>
2009-07-18 12:18     ` dharmendra pandit
2009-07-18 12:26       ` Richard Guenther
2009-07-18 12:31   ` Zoltán Kócsi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).