Thread: Structure Declarations

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    61

    Structure Declarations

    Hi There,

    A simple question but i have no idea how to do it

    if i do the following
    Code:
    #include......
    
    static struct FLEXCAN2_tag* const pCAN  = &CAN_B ;
    
    main()
    {
    pCAN->..........
    }
    it works

    but how do i do the following
    Code:
    #include......
    
    main()
    {
    static struct FLEXCAN2_tag* const pCAN  = &CAN_B ;
    pCAN->..........
    }
    reason im asking is because i want to achieve the wollowing in a function
    Code:
    void ReadMessage(port)
    {
    switch(port)
    case 0:
    pCAN = CAN_A
    break;
    
    case 1:
    pCAN = CAN-B
    break;
    
    pCAN->......
    }
    thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Remove the const qualifier and/or return the required value from the function.

    There are other possibilities (moving it to other places in the definition) but you haven't given enough information about what you're trying to achieve, so such suggestions may be inappropriate.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    Basically i am trying to programme a micro-controller with three identical ports, each port already has a structure associated with each CAN_A, CAN_B, CAN_C.

    To read and write to I simple have to CAN_A.ADDRESS = .....

    as all ports are the same in terms of reading and writing to i want to ideally have one function that does all three,

    ReadPort(Port Number)

    however i need a way of selecting which port, ideally
    Code:
    readPort(Port Number)
    {
    switch(Port Number)
    {
    case 0:
    pCAN = CAN_A;
    break;
    
    etc
    }
    
    pCAN->Address = ....
    }

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Then do something like this
    Code:
    /*  declarations of CAN_A and CAN_B must be global, so they exist outside readPort() */
    
    struct FLEXCAN2_tag *readPort(Port Number)
    {
        switch (Number)
        {
             case 0:    CAN_A.Address = whatever();
                           return &CAN_A;   /*  assume CAN_A and CAN_B are initialised structs */
                           break;
             case 1:    CAN_B.Address = wherever();
                           return &CAN_B;
        }
    }
    
    int main()
    {
          struct FLEXCAN2_tag *pCAN = readPort(0);   
    
           /*   Assume pCAN->Address is an int, and print */
    
          printf("%d\n", pCAN->Address);
    }
    I fail to see why ReadPort() needs to set the Address member - that should be possible in the definition of CAN_A and CAN_B.
    Code:
        struct FLEXCAN2_tag CAN_A = {  /*  initial values of members goes here */  };
    There are other ways you seem to be making life unnecessarily difficult for yourself, but you probably need to learn that for yourself rather than being told.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    Hi,

    I think i want to do something similar, like r_james14 i want to do something similar to the following,

    Code:
    int main()
    {
    switch()
    {
    case 0: 
    structFLEXCAN2_tag *RS232Port= PORT1;
    break;
    
    case 1:
    structFLEXCAN2_tag *RS232Port= port2;
    break;
    }
    
    SendRS232(RS232Port, 0x01, 0x02, 9600);
    }
    void SendRS232 (port, message part 1, message part 2, baudrate)
    {
     port -> RS232.baudrate;
     port -> RS232.message.message part 1
     port -> RS232.message.message part 2
     
    }
    but how do i initially code the switch statement at the beginning?

    I have taken a few strucutres out of this to make it easier to explain what im doing.

    PORT1 and PORT2 are prefefined structures

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Oh, for Pete's sake.

    I gave one approach (calling a function that returns a pointer) in Post #4 of this thread we're in which you clearly haven't even tried to understand.

    If you don't want to use a function, then the easiest approach is to separate the initialisation from the definition as per;
    Code:
    /*   Visible declarations of PORT1 and PORT2 as instances of struct FLEXCAN2_tag */
    
    struct FLEXCAN2_tag PORT1 = {};       /*   I assume you know how to initialise a struct, so haven't done that here*/
    struct FLEXCAN2_tag PORT2 = {};
    
    int main()
    {
         struct FLEXCAN2_tag *RS232Port;
        
          /*  initialise RS232Port */
    
         switch(whatever_you_are_switching_on)
         {
              case 0: RS232Port= &PORT1;  break;
              case 1: RS232Port= &PORT2;  break;
    
              default:    complain_about_unknown_port();
          }
    
          SendRS232(RS232Port, 0x01, 0x02, 9600);
    }
    With this approach you need to take care to ensure type matching. If the compiler complains about type mismatches (eg assigning a pointer to a non-pointer) then you are mixing things up with usage of ampersands and asterixes. If PORT1/2 are pointers, for example, the ampersand is not needed in the switch statement. C compilers are required by the standard to be fussy about such type mismatches.


    If you want to actually do the switching in the definition of RS232Port, you can't use a switch statement, and need to use the ternary ?: operator. Such approaches are moderately more difficult to get right so I won't give an example here.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declarations and Definitions
    By jackson6612 in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2011, 07:35 PM
  2. Declarations
    By slippy in forum C++ Programming
    Replies: 13
    Last Post: 12-01-2007, 05:02 PM
  3. portable declarations
    By Marv in forum C Programming
    Replies: 10
    Last Post: 04-12-2007, 04:39 PM
  4. help on declarations
    By robasc in forum C Programming
    Replies: 9
    Last Post: 03-05-2005, 01:50 PM
  5. non-external declarations...
    By headexplodes in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2002, 02:19 AM