[] equivalence?

Discussion in 'C++' started by Paul Williams, Jun 8, 2004.

  1. 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
     
    Paul Williams, Jun 8, 2004
    #1
    1. Advertising

  2. Paul Williams

    Sharad Kala Guest

    "Paul Williams" <> wrote in message
    news:...
    > 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
     
    Sharad Kala, Jun 8, 2004
    #2
    1. Advertising

  3. Paul Williams

    Andre Heinen Guest

    On 8 Jun 2004 04:46:17 -0700, (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"
     
    Andre Heinen, Jun 8, 2004
    #3
  4. Paul Williams

    Bill Seurer Guest

    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?
     
    Bill Seurer, Jun 8, 2004
    #4
  5. In message <>, Paul
    Williams <> 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
     
    Richard Herring, Jun 8, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ALuPin

    Re: Equivalence checking

    ALuPin, Apr 5, 2004, in forum: VHDL
    Replies:
    1
    Views:
    886
    Thomas Stanka
    Apr 6, 2004
  2. Forgafun

    Equivalence checkers for clocks

    Forgafun, Oct 1, 2005, in forum: VHDL
    Replies:
    1
    Views:
    484
    Ralf Hildebrandt
    Oct 1, 2005
  3. Rick Jackson

    Equivalence checking

    Rick Jackson, Nov 23, 2005, in forum: VHDL
    Replies:
    5
    Views:
    1,301
  4. MS News \(MS ILM\)
    Replies:
    0
    Views:
    380
    MS News \(MS ILM\)
    Aug 28, 2003
  5. =?Utf-8?B?S2VubmV0aCBQ?=

    MySql's equivalence to MsSql's 'TOP' command/function/clause

    =?Utf-8?B?S2VubmV0aCBQ?=, Jan 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    11,830
    John Timney \(ASP.NET MVP\)
    Jan 8, 2005
Loading...

Share This Page