Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C: warning: excess elements in array initializer

C: warning: excess elements in array initializer

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsdata-structures
14 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard Andrew x64R Richard Andrew x64

    Jochen Arndt wrote:

    C does not know references

    :omg: Are you sure? I used references in C when I was in school.

    The difficult we do right away... ...the impossible takes slightly longer.

    J Offline
    J Offline
    Jochen Arndt
    wrote on last edited by
    #4

    I must confess that I have some difficulties to reply. I should have explained it in another way because naming something that does not exist for a specific subject is problematic. C++ introduced the concept of a reference. These are mainly used for passing arguments. Then it is usually called pass-by-reference. This concept does not exist for C but the poster used C++ style function declarations using that C++ concept. With C, passing arguments by pointers was (and still is) also called pass-by-reference. But passing by pointer is technically passing by value.

    www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references[^]:

    In C, Pass-by-reference is simulated by passing the address of a variable (a pointer) and dereferencing that address within the function to read or write the actual variable. This will be referred to as "C style pass-by-reference."

    L Richard Andrew x64R 2 Replies Last reply
    0
    • J Jochen Arndt

      I must confess that I have some difficulties to reply. I should have explained it in another way because naming something that does not exist for a specific subject is problematic. C++ introduced the concept of a reference. These are mainly used for passing arguments. Then it is usually called pass-by-reference. This concept does not exist for C but the poster used C++ style function declarations using that C++ concept. With C, passing arguments by pointers was (and still is) also called pass-by-reference. But passing by pointer is technically passing by value.

      www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references[^]:

      In C, Pass-by-reference is simulated by passing the address of a variable (a pointer) and dereferencing that address within the function to read or write the actual variable. This will be referred to as "C style pass-by-reference."

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #5

      Jochen Arndt wrote:

      But passing by pointer is technically passing by value.

      No it is not. The pointer points to a variable, it is not the value, but its address (i.e. a reference to it). Consider:

      void PassByValue(int foo)
      {
      // the value of foo is passed directly and may be manipulated here
      int i = foo + 5;
      }

      void PassByPointer(int* pFoo)
      {
      // the value of foo must first be obtained by dereferencing pFoo
      int foo = *pFoo; // get the value into a local variable
      int i = foo + 5;
      }

      J 1 Reply Last reply
      0
      • L Lost User

        Jochen Arndt wrote:

        But passing by pointer is technically passing by value.

        No it is not. The pointer points to a variable, it is not the value, but its address (i.e. a reference to it). Consider:

        void PassByValue(int foo)
        {
        // the value of foo is passed directly and may be manipulated here
        int i = foo + 5;
        }

        void PassByPointer(int* pFoo)
        {
        // the value of foo must first be obtained by dereferencing pFoo
        int foo = *pFoo; // get the value into a local variable
        int i = foo + 5;
        }

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #6

        pFoo is a value of type int*. If you change pFoo inside the function, the value of pFoo will be changed (the address) but not the value of the variable it points to. Therefore, it is technically passing by value. Compare this with a reference parameter int& foo. If you change foo, the value of the referenced variable will be changed but not the hidden address.

        L 1 Reply Last reply
        0
        • J Jochen Arndt

          pFoo is a value of type int*. If you change pFoo inside the function, the value of pFoo will be changed (the address) but not the value of the variable it points to. Therefore, it is technically passing by value. Compare this with a reference parameter int& foo. If you change foo, the value of the referenced variable will be changed but not the hidden address.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #7

          Jochen Arndt wrote:

          pFoo is a value of type int*

          No it is a pointer type, not a value type. And a reference type (which does not exist in C) is just a shorthand allowing you to modify a caller's value. But under the covers it behaves just the same as a pointer, thus:

          void PassByPointer(int* pFoo)
          {
          *pFoo += 1; // manipulates whatever pFoo points to
          }

          void PassByReference(int& rFoo)
          {
          rFoo += 1; // manipulates whatever rFoo refers (i.e. points) to
          }

          J 1 Reply Last reply
          0
          • L Lost User

            Jochen Arndt wrote:

            pFoo is a value of type int*

            No it is a pointer type, not a value type. And a reference type (which does not exist in C) is just a shorthand allowing you to modify a caller's value. But under the covers it behaves just the same as a pointer, thus:

            void PassByPointer(int* pFoo)
            {
            *pFoo += 1; // manipulates whatever pFoo points to
            }

            void PassByReference(int& rFoo)
            {
            rFoo += 1; // manipulates whatever rFoo refers (i.e. points) to
            }

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #8

            A value is the content held by a variable. A pointer contains an address which is it's value.

            L 1 Reply Last reply
            0
            • J Jochen Arndt

              A value is the content held by a variable. A pointer contains an address which is it's value.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #9

              Semantics. ;P

              1 Reply Last reply
              0
              • J Jochen Arndt

                I must confess that I have some difficulties to reply. I should have explained it in another way because naming something that does not exist for a specific subject is problematic. C++ introduced the concept of a reference. These are mainly used for passing arguments. Then it is usually called pass-by-reference. This concept does not exist for C but the poster used C++ style function declarations using that C++ concept. With C, passing arguments by pointers was (and still is) also called pass-by-reference. But passing by pointer is technically passing by value.

                www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references[^]:

                In C, Pass-by-reference is simulated by passing the address of a variable (a pointer) and dereferencing that address within the function to read or write the actual variable. This will be referred to as "C style pass-by-reference."

                Richard Andrew x64R Offline
                Richard Andrew x64R Offline
                Richard Andrew x64
                wrote on last edited by
                #10

                I appreciate the clarification. However, in school we used the & to indicate a reference in C. As in:

                int PassByeReference(B& Parameter)

                { // Do something with B }

                The difficult we do right away... ...the impossible takes slightly longer.

                K 1 Reply Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  I appreciate the clarification. However, in school we used the & to indicate a reference in C. As in:

                  int PassByeReference(B& Parameter)

                  { // Do something with B }

                  The difficult we do right away... ...the impossible takes slightly longer.

                  K Offline
                  K Offline
                  k5054
                  wrote on last edited by
                  #11

                  Standard C does not know about references. Also, strict ANSI C does not know about // style comments, but many modern C compilers do. Possibly you were using a C++ compiler to compile c code?

                  Richard Andrew x64R 1 Reply Last reply
                  0
                  • K k5054

                    Standard C does not know about references. Also, strict ANSI C does not know about // style comments, but many modern C compilers do. Possibly you were using a C++ compiler to compile c code?

                    Richard Andrew x64R Offline
                    Richard Andrew x64R Offline
                    Richard Andrew x64
                    wrote on last edited by
                    #12

                    It was an early version of Borland Turbo C for DOS. Maybe they added the reference feature to the language?

                    The difficult we do right away... ...the impossible takes slightly longer.

                    K J 2 Replies Last reply
                    0
                    • Richard Andrew x64R Richard Andrew x64

                      It was an early version of Borland Turbo C for DOS. Maybe they added the reference feature to the language?

                      The difficult we do right away... ...the impossible takes slightly longer.

                      K Offline
                      K Offline
                      k5054
                      wrote on last edited by
                      #13

                      That seems unlikely. I tried the following here : https://archive.org/details/msdos\_borland\_turbo\_c\_2.01, and got the expected compilation error

                      #include void f(int &x) { x += 1; }

                      int main(void)
                      {
                      int n = 1;
                      printf("n = %d\n", n);
                      f(n);
                      printf("n = %d\n", n);

                      return 0;
                      

                      }

                      According to cppreference.com, references were added as early as 1985, and the compiler used above was 2.0, (c) 1990. Its not impossible that Borland added references before that, but them removed them later. Or added them at a later date when their C++ tools came out.

                      1 Reply Last reply
                      0
                      • Richard Andrew x64R Richard Andrew x64

                        It was an early version of Borland Turbo C for DOS. Maybe they added the reference feature to the language?

                        The difficult we do right away... ...the impossible takes slightly longer.

                        J Offline
                        J Offline
                        Jochen Arndt
                        wrote on last edited by
                        #14

                        I have learned C programming with Turbo C in the late 1980's and can't remember that it supported references. Turbo C was discontinued as stand alone product in 1990 and the C compiler was included with Turbo C++ 1.0. So you probably used that.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups