Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > [] equivalence?

Reply
Thread Tools

[] equivalence?

 
 
Paul Williams
Guest
Posts: n/a
 
      06-08-2004
I've got some char arrays declared, some as

char anArray[10] ;

and some as

char aChar ;

Im trying to ensure that all are null terminated, so am using:

anArray[0] = '\0' ;
aChar[0] = '\0' ;

But the second one does not compile - I get the error:

'The array operator must have one operand that is a pointer to
acomplete type and one of integral type'

I thought that aChar[n] was equivalent to (&aChar + n)?

Obviously aChar = '\0' ; works, but is that not equivalent to
aChar[0] = '\0' ;

ta
 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      06-08-2004

"Paul Williams" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I've got some char arrays declared, some as
>
> char anArray[10] ;
>
> and some as
>
> char aChar ;
>
> Im trying to ensure that all are null terminated, so am using:
>
> anArray[0] = '\0' ;
> aChar[0] = '\0' ;
>
> But the second one does not compile - I get the error:
>
> 'The array operator must have one operand that is a pointer to
> acomplete type and one of integral type'
>
> I thought that aChar[n] was equivalent to (&aChar + n)?


No, it's *(aChar + n)

> Obviously aChar = '\0' ; works, but is that not equivalent to
> aChar[0] = '\0' ;


Obviously, aChar[0] == *(aChar + 0), aChar is no pointer. Hence the error.
btw, there are array to pointer conversions. The result is a pointer to the
first element of the array.
Hence, anArray[0] == *( anArray + 0), here anArray is &anArray[0].

-Sharad





 
Reply With Quote
 
 
 
 
Andre Heinen
Guest
Posts: n/a
 
      06-08-2004
On 8 Jun 2004 04:46:17 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Paul
Williams) wrote:

>I've got some char arrays declared, some as
>char anArray[10] ;
>and some as
>char aChar ;


This is *not* an array of char. It is just a char. A single
one.

><snip>
>Obviously aChar = '\0' ; works,


Yes, because aChar is a char.

>but is that not equivalent to
>aChar[0] = '\0' ;


As aChar is not an array, you can't write aChar[0].

--
Andre Heinen
My address is "a dot heinen at europeanlink dot com"
 
Reply With Quote
 
Bill Seurer
Guest
Posts: n/a
 
      06-08-2004
Paul Williams wrote:

> I've got some char arrays declared, some as
>
> char anArray[10] ;
>
> and some as
>
> char aChar ;


That is not an array.

> Im trying to ensure that all are null terminated, so am using:
>
> anArray[0] = '\0' ;
> aChar[0] = '\0' ;
>
> But the second one does not compile - I get the error:
>
> 'The array operator must have one operand that is a pointer to
> acomplete type and one of integral type'
>
> I thought that aChar[n] was equivalent to (&aChar + n)?


No, arr[n] is equivalent to *(arr+n).

In any case aChar is not an array or pointer so you cannot using the
subscript operator on it. Why are you trying to do it this way anyway?
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      06-08-2004
In message <(E-Mail Removed) >, Paul
Williams <(E-Mail Removed)> writes
>I've got some char arrays declared, some as
>
>char anArray[10] ;
>
>and some as
>
>char aChar ;


That's not an array.
>
>Im trying to ensure that all are null terminated, so am using:
>
>anArray[0] = '\0' ;
>aChar[0] = '\0' ;
>
>But the second one does not compile - I get the error:
>
>'The array operator must have one operand that is a pointer to
>acomplete type and one of integral type'
>
>I thought that aChar[n] was equivalent to (&aChar + n)?


No, because aChar isn't of array type.

An lvalue or rvalue of array type can be converted to a pointer, so
anArray[n] is equivalent to * (&anArray[0] + n). Note the difference
from what you wrote above.

That conversion is only possible for arrays.

>
>Obviously aChar = '\0' ; works, but is that not equivalent to
>aChar[0] = '\0' ;
>


--
Richard Herring
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments