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. msvc struct initialisation query

msvc struct initialisation query

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasedata-structures
3 Posts 3 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.
  • J Offline
    J Offline
    Jon Hulatt
    wrote on last edited by
    #1

    imagine a struct declared as follows:-

    typedef struct
    {
    int integerArray[];
    } testStruct;

    I can instantiate like this:-

    testStruct a = {
    {1,2,3}
    };

    Which is all well and good. But, if the struct declaration is changed to an int* member:-

    typedef struct
    {
    int *integerArray;
    } testStruct;

    How can I now instantiate it so that integerArray points to my array of {1,2,3} ?? in gcc, you can cast {1,2,3} to (int *), as follows:-

    testStruct a = {
    (int *){1,2,3}
    };

    But msvc doesn't allow that. Is there another way to make this instantiation? thanks Jon

    using System.Beer;

    D 1 Reply Last reply
    0
    • J Jon Hulatt

      imagine a struct declared as follows:-

      typedef struct
      {
      int integerArray[];
      } testStruct;

      I can instantiate like this:-

      testStruct a = {
      {1,2,3}
      };

      Which is all well and good. But, if the struct declaration is changed to an int* member:-

      typedef struct
      {
      int *integerArray;
      } testStruct;

      How can I now instantiate it so that integerArray points to my array of {1,2,3} ?? in gcc, you can cast {1,2,3} to (int *), as follows:-

      testStruct a = {
      (int *){1,2,3}
      };

      But msvc doesn't allow that. Is there another way to make this instantiation? thanks Jon

      using System.Beer;

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Jon Hulatt wrote:

      Is there another way to make this instantiation?

      How about:

      testStruct a = { new int(1), new int(2), new int(3) };

      "Love people and use things, not love things and use people." - Unknown

      "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

      CPalliniC 1 Reply Last reply
      0
      • D David Crow

        Jon Hulatt wrote:

        Is there another way to make this instantiation?

        How about:

        testStruct a = { new int(1), new int(2), new int(3) };

        "Love people and use things, not love things and use people." - Unknown

        "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Very clever, anyway I suspect he wants the allocation to happen onto the stack. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        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