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. What's the closest thing to an anonymous struct/array I can achieve that compiles with GCC?

What's the closest thing to an anonymous struct/array I can achieve that compiles with GCC?

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpcomdata-structures
11 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.
  • A arnold_w

    I am writing code for an ARM processor and compiling with GCC. I have done some C# programming and there you're allowed to pass anonymous arrays and structs directly in method calls:

    MyClass.MyMethod1(new byte[]{0, 1, 2, 3}); // This is allowed in C#

    and

    MyClass.MyMethod2(new MyStruct("", 0, null)); // This is allowed in C#

    What's the closest thing I can achieve the same thing in my ARM-project?

    myFunction1({0, 1, 2, 3}); // What I want (or something similar) that compiles in GCC
    myFunction2({"", 0, NULL}); // What I want (or something similar) that compiles in GCC

    I understand that I could change my function to take a function pointer instead of a struct as a parameter using the technique described in Re: What's the closest thing to anonymous function pointers I can achieve that compiles with GCC? - C / C++ / MFC Discussion Boards[^]:

    #include
    using namespace std;
    using getMyStructCallback_t = function ;

    void myFunction(getMyStructCallback_t getMyStructCallback); // Declaration as it appears in the h-file

    myFunction([]() {
    static struct MyStruct myStruct = {"", 0, NULL}; // What I'm aware is possible, but would like to avoid
    return &myStruct;} // What I'm aware is possible, but would like to avoid
    );

    but if possible, then I would like to pass my struct/array directly.

    Mircea NeacsuM Offline
    Mircea NeacsuM Offline
    Mircea Neacsu
    wrote on last edited by
    #2

    Check this:

    struct MyStruct
    {
    const char* s;
    int i;
    void* ptr;
    };

    class MyClass
    {
    public:
    MyClass ();
    void MyMehod1 (const std::vector& v) {};
    void MyMethod2 (const MyStruct& s) {};
    };

    int main()
    {

    MyClass c;
    c.MyMehod1 ({ 0, 1, 2, 3 });
    c.MyMethod2 ({ "", 0, nullptr });
    }

    Mircea

    A 1 Reply Last reply
    0
    • Mircea NeacsuM Mircea Neacsu

      Check this:

      struct MyStruct
      {
      const char* s;
      int i;
      void* ptr;
      };

      class MyClass
      {
      public:
      MyClass ();
      void MyMehod1 (const std::vector& v) {};
      void MyMethod2 (const MyStruct& s) {};
      };

      int main()
      {

      MyClass c;
      c.MyMehod1 ({ 0, 1, 2, 3 });
      c.MyMethod2 ({ "", 0, nullptr });
      }

      Mircea

      A Offline
      A Offline
      arnold_w
      wrote on last edited by
      #3

      Is it possible to do without classes/object orientedness?

      Mircea NeacsuM 1 Reply Last reply
      0
      • A arnold_w

        Is it possible to do without classes/object orientedness?

        Mircea NeacsuM Offline
        Mircea NeacsuM Offline
        Mircea Neacsu
        wrote on last edited by
        #4

        Your question had this code:

        MyClass.MyMethod1(new byte[]{0, 1, 2, 3});

        Here you a calling a method (also called member function) of an object. So your code is already object oriented. Maybe you should revise/clarify what you want to accomplish.

        Mircea

        A 1 Reply Last reply
        0
        • Mircea NeacsuM Mircea Neacsu

          Your question had this code:

          MyClass.MyMethod1(new byte[]{0, 1, 2, 3});

          Here you a calling a method (also called member function) of an object. So your code is already object oriented. Maybe you should revise/clarify what you want to accomplish.

          Mircea

          A Offline
          A Offline
          arnold_w
          wrote on last edited by
          #5

          That was a C# example, to indicate that I was looking for the corresponding C/C++ syntax for my ARM-project. I try to keep my ARM-code as close to pure C-programming as possible (this will obviously be an exception) and never use classes in the ARM-project. Is anything in your example dependent on the classes or will it compile just fine without classes? I'm not at work so I can't try it for myself right now, but I plan to work on this first thing in the morning.

          L 1 Reply Last reply
          0
          • A arnold_w

            I am writing code for an ARM processor and compiling with GCC. I have done some C# programming and there you're allowed to pass anonymous arrays and structs directly in method calls:

            MyClass.MyMethod1(new byte[]{0, 1, 2, 3}); // This is allowed in C#

            and

            MyClass.MyMethod2(new MyStruct("", 0, null)); // This is allowed in C#

            What's the closest thing I can achieve the same thing in my ARM-project?

            myFunction1({0, 1, 2, 3}); // What I want (or something similar) that compiles in GCC
            myFunction2({"", 0, NULL}); // What I want (or something similar) that compiles in GCC

            I understand that I could change my function to take a function pointer instead of a struct as a parameter using the technique described in Re: What's the closest thing to anonymous function pointers I can achieve that compiles with GCC? - C / C++ / MFC Discussion Boards[^]:

            #include
            using namespace std;
            using getMyStructCallback_t = function ;

            void myFunction(getMyStructCallback_t getMyStructCallback); // Declaration as it appears in the h-file

            myFunction([]() {
            static struct MyStruct myStruct = {"", 0, NULL}; // What I'm aware is possible, but would like to avoid
            return &myStruct;} // What I'm aware is possible, but would like to avoid
            );

            but if possible, then I would like to pass my struct/array directly.

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #6

            You could do something like:

            void Function(const std::vector& values)

            Call it as follows:

            Function(std::vector{ 5, 4, 3});

            (Works with VC++ 2017. Should work with GCC.)

            A 1 Reply Last reply
            0
            • A arnold_w

              That was a C# example, to indicate that I was looking for the corresponding C/C++ syntax for my ARM-project. I try to keep my ARM-code as close to pure C-programming as possible (this will obviously be an exception) and never use classes in the ARM-project. Is anything in your example dependent on the classes or will it compile just fine without classes? I'm not at work so I can't try it for myself right now, but I plan to work on this first thing in the morning.

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

              arnold_w wrote:

              I try to keep my ARM-code as close to pure C

              arnold_w wrote:

              I plan to work on this first thing in the morning.

              When you get back into the office please print out __STDC_VERSION__ so we know which language version you are working under. The C99 language and below had no support for anonymous structures or unions. But the GCC compiler has a non-standard extension -std=gnu99 that enables it. Support for anonymous structures or unions was added in C11[^] back in 2011 If your __STDC_VERSION__ is 199901L or above then you should be able to use anonymous objects. Best Wishes, -David Delaune

              1 Reply Last reply
              0
              • J Joe Woodbury

                You could do something like:

                void Function(const std::vector& values)

                Call it as follows:

                Function(std::vector{ 5, 4, 3});

                (Works with VC++ 2017. Should work with GCC.)

                A Offline
                A Offline
                arnold_w
                wrote on last edited by
                #8

                Joe Woodbury wrote:

                void Function(const std::vector<uint8_t>& values)

                Is there anything preventing me from having an override pure C-function (in addition to your C++ function) as follows:

                void Function(uint8_t* values)

                ? I'm thinking that in that case I would have functions that can be called both from files with .c-extension and .cpp-extension? I'd image const std::vector stores bytes in memory exactly the same way uint8_t* (or uint8_t[]) does, right?

                J S 2 Replies Last reply
                0
                • A arnold_w

                  Joe Woodbury wrote:

                  void Function(const std::vector<uint8_t>& values)

                  Is there anything preventing me from having an override pure C-function (in addition to your C++ function) as follows:

                  void Function(uint8_t* values)

                  ? I'm thinking that in that case I would have functions that can be called both from files with .c-extension and .cpp-extension? I'd image const std::vector stores bytes in memory exactly the same way uint8_t* (or uint8_t[]) does, right?

                  J Offline
                  J Offline
                  Joe Woodbury
                  wrote on last edited by
                  #9

                  In C++, you could overload Function, but you'd have to do the normal #ifdef stuff to call Function from C. (Yes, by standard std::vector stores data contiguously.) The problem is that the C function is rather dangerous.

                  1 Reply Last reply
                  0
                  • A arnold_w

                    I am writing code for an ARM processor and compiling with GCC. I have done some C# programming and there you're allowed to pass anonymous arrays and structs directly in method calls:

                    MyClass.MyMethod1(new byte[]{0, 1, 2, 3}); // This is allowed in C#

                    and

                    MyClass.MyMethod2(new MyStruct("", 0, null)); // This is allowed in C#

                    What's the closest thing I can achieve the same thing in my ARM-project?

                    myFunction1({0, 1, 2, 3}); // What I want (or something similar) that compiles in GCC
                    myFunction2({"", 0, NULL}); // What I want (or something similar) that compiles in GCC

                    I understand that I could change my function to take a function pointer instead of a struct as a parameter using the technique described in Re: What's the closest thing to anonymous function pointers I can achieve that compiles with GCC? - C / C++ / MFC Discussion Boards[^]:

                    #include
                    using namespace std;
                    using getMyStructCallback_t = function ;

                    void myFunction(getMyStructCallback_t getMyStructCallback); // Declaration as it appears in the h-file

                    myFunction([]() {
                    static struct MyStruct myStruct = {"", 0, NULL}; // What I'm aware is possible, but would like to avoid
                    return &myStruct;} // What I'm aware is possible, but would like to avoid
                    );

                    but if possible, then I would like to pass my struct/array directly.

                    A Offline
                    A Offline
                    arnold_w
                    wrote on last edited by
                    #10

                    Today I saw the following syntax:

                    uint16_t getUint16Value(int someParameter) {
                    // Unimportant implementation
                    }

                    void myFunction() {
                    uint16_t destArray[2];
                    memcpy(destArray, (uint16_t[2]){getUint16Value(0), getUint16Value(1)}, sizeof(uint16_t[2]));
                    }

                    Now, what made me really surprized about this code is that it was inside a .c-file, not a .cpp-file! I looked in the GCC documentation and tried to find examples of clever syntax like this, but I couldn't find any. Does anybody know where I can examples of anonymuous "things" (arrays, structs, function-pointers, etc) that GCC allows in C-code?

                    1 Reply Last reply
                    0
                    • A arnold_w

                      Joe Woodbury wrote:

                      void Function(const std::vector<uint8_t>& values)

                      Is there anything preventing me from having an override pure C-function (in addition to your C++ function) as follows:

                      void Function(uint8_t* values)

                      ? I'm thinking that in that case I would have functions that can be called both from files with .c-extension and .cpp-extension? I'd image const std::vector stores bytes in memory exactly the same way uint8_t* (or uint8_t[]) does, right?

                      S Offline
                      S Offline
                      Stefan_Lang
                      wrote on last edited by
                      #11

                      Note that C arrays do not include length information! So unless your function Function(uint8_t*) somehow knows how many elements there are, you'd also have to pass the number of elements as a separate parameter! And even if it does know how many elements to expect, there's always a chance someone calls it accidentally with a different number of elements. The class std::vector behaves like an array, but internally it does store the array length which makes the code examples above feasible. You could of course write a C++ wrapper that calls a C function internally, like this:

                      void Function(uint8_t* values, unsigned long int size); //normal C function implemented elsewhere
                      void Function(const std::vector& values) {
                      Function(values.data(), values.size());
                      }
                      int main() {
                      Function({1, 2, 3});
                      return 0;
                      }

                      Otherwise, you'd have to think of another way to pass the element number reliably.

                      GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                      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