[] equivalence?

P

Paul Williams

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
 
S

Sharad Kala

Paul Williams said:
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
 
A

Andre Heinen

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

Bill Seurer

Paul said:
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?
 
R

Richard Herring

Paul said:
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' ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top