Reinitialization of same variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Orane
    New Member
    • Jul 2007
    • 1

    #1

    Reinitialization of same variable

    When I reintialize a variable, CL compiler issues the following error.

    ---- Program ---
    int siVar=10 ;
    int siVar=10;
    ---------------------

    error C2374: 'siVar' : redefinition; multiple initialization
    test.c(1) : see declaration of 'siVar'

    Can anyone tell me whether it is as per ANSI C specification.

    If yes, could you please give me the snip from ANSI C specification,( ISO/IEC 9899:1999)


    Thanks in advance :-)
  • ravenspoint
    New Member
    • Jul 2007
    • 111

    #2
    Originally posted by Orane
    When I reintialize a variable, CL compiler issues the following error.

    ---- Program ---
    int siVar=10 ;
    int siVar=10;
    ---------------------

    error C2374: 'siVar' : redefinition; multiple initialization
    test.c(1) : see declaration of 'siVar'
    You have told the compiler to store siVar in two different places. It rightly refuses.

    Declare the variable, with or without an initiazation, just once. Then you can only change its value.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by ravenspoint
      You have told the compiler to store siVar in two different places. It rightly refuses.

      Declare the variable, with or without an initiazation, just once. Then you can only change its value.
      No, that's not how thing work:you can declare a variable as many times as you
      want (all declarations should be identical), but you should only define it once.

      As the OP did: the variable was defined because of the initializer (list) and then
      it was defined again (using an identical initializer (list)).

      Check the ANSI/ISO Standard for the distinction between declaration and definition.

      Jos

      Comment

      • ravenspoint
        New Member
        • Jul 2007
        • 111

        #4
        Is that right? I confess I have always been vague about the difference between 'define' and 'declare'

        Sorry abut that.

        The thing to keep in mind is that when you write something like

        int x;

        the compiler allocates some memory where it keeps the value of x.

        You cannot now tell it to store another variable with the same name somewhere else in memory.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by ravenspoint
          Is that right? I confess I have always been vague about the difference between 'define' and 'declare'
          Define means to allocate memory. So these are definitions:
          [code=cpp]
          int data;

          void fx()
          {
          //etc...
          }
          [/code]

          Declare is to assert that something not defined does exist and is vaid to use. These are declarations:
          [code=cpp]
          extern int data;

          void fx();

          class MyClass
          {
          //etc...
          };
          [/code]

          Comment

          Working...