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. problems with pointer to struct

problems with pointer to struct

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancehelptutorial
6 Posts 4 Posters 2 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.
  • M Offline
    M Offline
    Mohammad Ali Bahar
    wrote on last edited by
    #1

    Hi every one. I have typical structure (ECU_t) which has members that considered to be pointer to struct. i make my question in comments. please help me. by the way i have used freescale code warrior as compiler.

    typedef enum
    {
    NORMAL,
    WARNING,
    }WATER_TEMP;

    typedef struct
    {
    WATER_TEMP water_temp;
    uint16_t speed;
    // ... other members
    }Engine_t;

    typedef struct
    {
    Engine_t* engine;
    ABS_t* abs;
    // ... other members
    }ECU_t;

    void ECU_DoTask(ECU_t* output)
    {
    output->engine->water_temp = NORMAL;

    }

    void main ()
    {
    ECU_t ecu;
    ECU_DoTask(&ecu);
    // i want to access to water_temp. how can i do it?
    // for example: ecu.engine->water_temp ????
    while(1);
    }

    CPalliniC L J 3 Replies Last reply
    0
    • M Mohammad Ali Bahar

      Hi every one. I have typical structure (ECU_t) which has members that considered to be pointer to struct. i make my question in comments. please help me. by the way i have used freescale code warrior as compiler.

      typedef enum
      {
      NORMAL,
      WARNING,
      }WATER_TEMP;

      typedef struct
      {
      WATER_TEMP water_temp;
      uint16_t speed;
      // ... other members
      }Engine_t;

      typedef struct
      {
      Engine_t* engine;
      ABS_t* abs;
      // ... other members
      }ECU_t;

      void ECU_DoTask(ECU_t* output)
      {
      output->engine->water_temp = NORMAL;

      }

      void main ()
      {
      ECU_t ecu;
      ECU_DoTask(&ecu);
      // i want to access to water_temp. how can i do it?
      // for example: ecu.engine->water_temp ????
      while(1);
      }

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

      your remarks show the correct way to access the variable, but (and it is a big 'but') the memory for the pointed struct must be first allocated. try

      int main ()
      {
      ECU_t ecu;
      ecu.engine = (Engine_t *) malloc(sizeof(ecu.engine)); // allocate memory fot the Engine_t struct
      ECU_DoTask(&ecu);
      printf("water_temp = %d\n", ecu.engine->water_temp);
      free( ecu.engine); // release allocated dynamic memory

      return 0;
      }

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • M Mohammad Ali Bahar

        Hi every one. I have typical structure (ECU_t) which has members that considered to be pointer to struct. i make my question in comments. please help me. by the way i have used freescale code warrior as compiler.

        typedef enum
        {
        NORMAL,
        WARNING,
        }WATER_TEMP;

        typedef struct
        {
        WATER_TEMP water_temp;
        uint16_t speed;
        // ... other members
        }Engine_t;

        typedef struct
        {
        Engine_t* engine;
        ABS_t* abs;
        // ... other members
        }ECU_t;

        void ECU_DoTask(ECU_t* output)
        {
        output->engine->water_temp = NORMAL;

        }

        void main ()
        {
        ECU_t ecu;
        ECU_DoTask(&ecu);
        // i want to access to water_temp. how can i do it?
        // for example: ecu.engine->water_temp ????
        while(1);
        }

        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #3

        Each structure inside is a pointer and needs to be allocated and even worse this is inherently dangerous because any of those pointers could be NULL if an error occurred and that line will crash.

        output->engine->water_temp = NORMAL;

        What you are building is a database and as the entries are small there is no advantages to allocation and pointers, you only do pointers if the data is going to be large. Just place the structure in as is.

        typedef enum
        {
        NORMAL = 0,
        WARNING = 1,
        } WATER_TEMP;

        typedef struct {
        WATER_TEMP water_temp;
        uint16_t speed;
        // ... other members
        } Engine_t;

        typedef struct
        {
        Engine_t engine; // <== Not a pointer anymore
        // ... other members
        } ECU_t;

        int main(void)
        {
        /* This creates a test instance .. requires C99 or higher wont work on old C89 compiler */
        ECU_t ecu_test = { .engine.water_temp = NORMAL,
        .engine.speed = 10 };

            /\* You access them in the normal way \*/
        printf("Water temp is %i\\n", ecu\_test.engine.water\_temp);
        printf("Engine speed is %i\\n", (unsigned int)ecu\_test.engine.speed);
        

        }

        In vino veritas

        M 1 Reply Last reply
        0
        • L leon de boer

          Each structure inside is a pointer and needs to be allocated and even worse this is inherently dangerous because any of those pointers could be NULL if an error occurred and that line will crash.

          output->engine->water_temp = NORMAL;

          What you are building is a database and as the entries are small there is no advantages to allocation and pointers, you only do pointers if the data is going to be large. Just place the structure in as is.

          typedef enum
          {
          NORMAL = 0,
          WARNING = 1,
          } WATER_TEMP;

          typedef struct {
          WATER_TEMP water_temp;
          uint16_t speed;
          // ... other members
          } Engine_t;

          typedef struct
          {
          Engine_t engine; // <== Not a pointer anymore
          // ... other members
          } ECU_t;

          int main(void)
          {
          /* This creates a test instance .. requires C99 or higher wont work on old C89 compiler */
          ECU_t ecu_test = { .engine.water_temp = NORMAL,
          .engine.speed = 10 };

              /\* You access them in the normal way \*/
          printf("Water temp is %i\\n", ecu\_test.engine.water\_temp);
          printf("Engine speed is %i\\n", (unsigned int)ecu\_test.engine.speed);
          

          }

          In vino veritas

          M Offline
          M Offline
          Mohammad Ali Bahar
          wrote on last edited by
          #4

          thank you. but i want to create a function with form of:

          void ECU_DoTask(ECU_t* out_ecu)
          {
          // perform out_ecu->engine->speed = ...
          // perform out_ecu->abs->status = ...
          // perform out_ecu->bcm->high_beam_status = ...
          }

          because i should pass the values of ecu parameters as an output argument of function. so i need use pointers.

          L 1 Reply Last reply
          0
          • M Mohammad Ali Bahar

            thank you. but i want to create a function with form of:

            void ECU_DoTask(ECU_t* out_ecu)
            {
            // perform out_ecu->engine->speed = ...
            // perform out_ecu->abs->status = ...
            // perform out_ecu->bcm->high_beam_status = ...
            }

            because i should pass the values of ecu parameters as an output argument of function. so i need use pointers.

            L Offline
            L Offline
            leon de boer
            wrote on last edited by
            #5

            You are missing the point nothing I have done stops you using pointers on the interface :-) In my code above you could do

            /* This is called a static allocation */
            /* You use the & symbol to make a pointer to the allocation */
            static ECU_t ecu_test;
            ECU_DoTask(&ecu_test);

            Or you could do this

            /* This is called a dynamic allocation */
            /* malloc makes space for the structure on the heap and gives you a pointer */
            ECU_t* ecu_test = malloc(sizeof(ECU_t));
            ECU_DoTask(ecu_test);

            However none of that has anything remotely to do with what is in the structure and removing the pointers and internally you have no way to know how I did the allocation externally :-) I think what has thrown you is me creating one of your structures without having to create it like you want, but that is just me being tricky with C literals to give you something to test.

            In vino veritas

            1 Reply Last reply
            0
            • M Mohammad Ali Bahar

              Hi every one. I have typical structure (ECU_t) which has members that considered to be pointer to struct. i make my question in comments. please help me. by the way i have used freescale code warrior as compiler.

              typedef enum
              {
              NORMAL,
              WARNING,
              }WATER_TEMP;

              typedef struct
              {
              WATER_TEMP water_temp;
              uint16_t speed;
              // ... other members
              }Engine_t;

              typedef struct
              {
              Engine_t* engine;
              ABS_t* abs;
              // ... other members
              }ECU_t;

              void ECU_DoTask(ECU_t* output)
              {
              output->engine->water_temp = NORMAL;

              }

              void main ()
              {
              ECU_t ecu;
              ECU_DoTask(&ecu);
              // i want to access to water_temp. how can i do it?
              // for example: ecu.engine->water_temp ????
              while(1);
              }

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

              Why bother making Engine_t and ABS_t pointers? Just make them members.

              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