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. how to use a struct pointer to get a struct pointer array's member

how to use a struct pointer to get a struct pointer array's member

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
8 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.
  • S Offline
    S Offline
    samzcs
    wrote on last edited by
    #1

    typedef struct {
    int s1;
    int s2;
    int s3
    }strTst;

    strTst spst1 = {3,5,7};

    strTst spst2 = {1,2,4};

    strTst *Sp1[2] =
    {
    &spst1,
    &spst2
    };

    strTst *pt = &Sp1;

    I wonder how can i use pt to get a value in spst2, I checked, (*pt) value = &spst1, *(pt+1) = &spst2. but the compiler not let me to use (*pt)->s1, or ( (strTst *)(*pt) )->s1

    Greg UtasG Mircea NeacsuM L M 4 Replies Last reply
    0
    • S samzcs

      typedef struct {
      int s1;
      int s2;
      int s3
      }strTst;

      strTst spst1 = {3,5,7};

      strTst spst2 = {1,2,4};

      strTst *Sp1[2] =
      {
      &spst1,
      &spst2
      };

      strTst *pt = &Sp1;

      I wonder how can i use pt to get a value in spst2, I checked, (*pt) value = &spst1, *(pt+1) = &spst2. but the compiler not let me to use (*pt)->s1, or ( (strTst *)(*pt) )->s1

      Greg UtasG Offline
      Greg UtasG Offline
      Greg Utas
      wrote on last edited by
      #2

      The last line won't compile: Sp1 is an array of 2 pointers to strTst instances, not a strTst*. To access s1 in spst2, you need

      strTst *pt = Sp1[1];
      pt->s1 = 0;

      Robust Services Core | Software Techniques for Lemmings | Articles
      The fox knows many things, but the hedgehog knows one big thing.

      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

      S 2 Replies Last reply
      0
      • Greg UtasG Greg Utas

        The last line won't compile: Sp1 is an array of 2 pointers to strTst instances, not a strTst*. To access s1 in spst2, you need

        strTst *pt = Sp1[1];
        pt->s1 = 0;

        Robust Services Core | Software Techniques for Lemmings | Articles
        The fox knows many things, but the hedgehog knows one big thing.

        S Offline
        S Offline
        samzcs
        wrote on last edited by
        #3

        it's compiled, but with a warning, incompatible pointer types Initializating

        1 Reply Last reply
        0
        • Greg UtasG Greg Utas

          The last line won't compile: Sp1 is an array of 2 pointers to strTst instances, not a strTst*. To access s1 in spst2, you need

          strTst *pt = Sp1[1];
          pt->s1 = 0;

          Robust Services Core | Software Techniques for Lemmings | Articles
          The fox knows many things, but the hedgehog knows one big thing.

          S Offline
          S Offline
          samzcs
          wrote on last edited by
          #4

          Can I cast the strTst* pt to an array of 2 pointers to strTst instance? with

          strTst *pt = &Sp1;

          although, there is a warning compiler issued. I get the address of the Sp1 array. I think since i get the address of a data object,then i can operate on the data object.

          Greg UtasG 1 Reply Last reply
          0
          • S samzcs

            Can I cast the strTst* pt to an array of 2 pointers to strTst instance? with

            strTst *pt = &Sp1;

            although, there is a warning compiler issued. I get the address of the Sp1 array. I think since i get the address of a data object,then i can operate on the data object.

            Greg UtasG Offline
            Greg UtasG Offline
            Greg Utas
            wrote on last edited by
            #5

            A cast should only be used when necessary, which isn't the case here. EDIT: I'm compiling under C++, so maybe it's an error there, and only a warning in C.

            Robust Services Core | Software Techniques for Lemmings | Articles
            The fox knows many things, but the hedgehog knows one big thing.

            <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
            <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

            1 Reply Last reply
            0
            • S samzcs

              typedef struct {
              int s1;
              int s2;
              int s3
              }strTst;

              strTst spst1 = {3,5,7};

              strTst spst2 = {1,2,4};

              strTst *Sp1[2] =
              {
              &spst1,
              &spst2
              };

              strTst *pt = &Sp1;

              I wonder how can i use pt to get a value in spst2, I checked, (*pt) value = &spst1, *(pt+1) = &spst2. but the compiler not let me to use (*pt)->s1, or ( (strTst *)(*pt) )->s1

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

              How about this:

              typedef struct {
              int s1;
              int s2;
              int s3;
              }strTst;

              strTst spst1 = { 3,5,7 };

              strTst spst2 = { 1,2,4 };

              strTst* Sp1[2] =
              {
              &spst1,
              &spst2
              };

              strTst** pt = Sp1;

              void f () {
              pt[1]->s1 = 0;
              }

              Mircea

              1 Reply Last reply
              0
              • S samzcs

                typedef struct {
                int s1;
                int s2;
                int s3
                }strTst;

                strTst spst1 = {3,5,7};

                strTst spst2 = {1,2,4};

                strTst *Sp1[2] =
                {
                &spst1,
                &spst2
                };

                strTst *pt = &Sp1;

                I wonder how can i use pt to get a value in spst2, I checked, (*pt) value = &spst1, *(pt+1) = &spst2. but the compiler not let me to use (*pt)->s1, or ( (strTst *)(*pt) )->s1

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

                You have declared Sp1 to be an array of pointers, each of which points to a strTst structure. So to get one of the pointers you just need normal array addressing, e.g. Sp1[1] returns the second element of the array. So all you need is:

                strTst *pt = Sp1[1]; // pt now equals &spst2
                int value = pt->s2; // get the value of spst2.s2

                1 Reply Last reply
                0
                • S samzcs

                  typedef struct {
                  int s1;
                  int s2;
                  int s3
                  }strTst;

                  strTst spst1 = {3,5,7};

                  strTst spst2 = {1,2,4};

                  strTst *Sp1[2] =
                  {
                  &spst1,
                  &spst2
                  };

                  strTst *pt = &Sp1;

                  I wonder how can i use pt to get a value in spst2, I checked, (*pt) value = &spst1, *(pt+1) = &spst2. but the compiler not let me to use (*pt)->s1, or ( (strTst *)(*pt) )->s1

                  M Offline
                  M Offline
                  magnomagna
                  wrote on last edited by
                  #8

                  strTst *pt = Sp1[0];

                  pt->s1; // is spst1.s1
                  pt->s2; // is spst1.s2
                  pt->s3; // is spst1.s3

                  pt = Sp1[1];

                  pt->s1; // is spst2.s1
                  pt->s2; // is spst2.s2
                  pt->s3; // is spst2.s3

                  // alternative

                  strTst **ppt = Sp1;

                  ppt[0]->s1; // is spst1.s1
                  ppt[0]->s2; // is spst1.s2
                  ppt[0]->s3; // is spst1.s3

                  ppt[1]->s1; // is spst2.s1
                  ppt[1]->s2; // is spst2.s2
                  ppt[1]->s3; // is spst2.s3

                  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