Thread: struct and pointers

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    struct and pointers

    Hi,

    I am trying to create different messages to be transferred between two hosts. The program structure that I am following is to create a master struct where all the variables to be passed in different messages are declared.
    Then I have created different struct for different messages using the same variables from master struct, the values of which I am trying to pass is through pointers.
    Below is the rough outline of what I am trying to do. I dont know whether this is the right approach or there is another better way to do it.

    The errors that I am getting while compiling are:
    1. expression must have a constant value
    2. incomplete type is not allowed

    Both are pointed when I try to create an instance of message structure and passing the pointers of master struct.

    Please help.

    Code:
    struct Master{
    
      char  Bulk_Charging_Complete : 1;
      char  Charging_Complete : 1;
      short Vehicle_Energy_Capacity;
      char  Vehicle_RESS_SOC;
      short Vehicle_Maximum_Power_Limit : 13;
      short Vehicle_Maximum_Current_Limit: 13;
      short Vehicle_Maximum_Voltage_Limit: 13;
      short Charger_Maximum_Power_Limit: 13;
      short Charger_Maximum_Current_Limit: 13;
      short Charger_Maximum_Voltage_Limit: 14;
      short Charge_Current_Request: 13;
    } Power;
    
    
     /* create a pointer of type Master struct and point to instance of that struct type,i.e Power */
    
    
    struct Master *power_pointer = &Power;     
    
    
    // creating a structure of a message, the value to its variables will be fetched by pointer to the master structure
    
    struct {
      short var1 : 1;
      char var2 : 4;
      short var3 : 1;
      short var4 : 4;  
    }EV_msg_01;
    
    // creating a structure of a message, the value to its variables will be fetched by pointer to the master structure
    
    struct {
      short var1 : 1;
      short var2 : 4;
      short var3 : 1;
      short var4 : 4;  
    }station_msg_01;
    
    
    
    struct EV_msg  EV_msg_01 = {
                              power_pointer->Vehicle_Energy_Capacity,         
                              power_pointer->Vehicle_RESS_SOC, 
                              power_pointer->Vehicle_Maximum_Power_Limit,     
                              power_pointer->Vehicle_Maximum_Current_Limit
                              };
    
    
    struct station_msg_01 station_msg_01 = {
                               power_pointer->Charger_Maximum_Power_Limit,  
                               power_pointer->Charger_Maximum_Current_Limit, 
                               power_pointer->Charger_Maximum_Voltage_Limit,  
                               power_pointer->Charge_Current_Request
                              };

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your struct prototype should be placed just after your include files and function prototypes. That way all your other functions have that global proto info on the struct. The structs you actually work on, should be declared in main(), and then passed around via pointers to your other functions.

    Inside those other functions, you can now declare pointers to a local struct, if you wish.

    What's with the colons? Is this code from Pascal or ???

    Change your editor you code with, so it doesn't put tabs into the code - use spaces instead. Your code, as it's displayed on the forum, is almost unreadable. By the time I get to the end of a line of code, the first part of the line, is no longer visible. (This is true with all the programming forums I've been to, btw.)

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Adak, the colons are for bit-fields. They are valid C, although I suspect being misused in this case.

    Quote Originally Posted by Sunjune View Post
    Both are pointed when I try to create an instance of message structure and passing the pointers of master struct.
    That is your basic problem. You are not allowed to do that.

    If you want to initialise a struct, you need to provide values that are known to the compiler at compile time. The members of another struct are not known at compile time. Hence the message about "expression must have a constant value".

    Expressions like power_pointer->Vehicle_Energy_Capacity are not compile-time constants.
    Quote Originally Posted by Sunjune View Post
    Code:
    struct EV_msg  EV_msg_01 = {
                              power_pointer->Vehicle_Energy_Capacity,         
                              power_pointer->Vehicle_RESS_SOC, 
                              power_pointer->Vehicle_Maximum_Power_Limit,     
                              power_pointer->Vehicle_Maximum_Current_Limit
                              };
    
    
    struct station_msg_01 station_msg_01 = {
                               power_pointer->Charger_Maximum_Power_Limit,  
                               power_pointer->Charger_Maximum_Current_Limit, 
                               power_pointer->Charger_Maximum_Voltage_Limit,  
                               power_pointer->Charge_Current_Request
                              };
    Take out the lines above. Apart from the fact (that I mentioned above) these are trying to initialise a struct with variables unknown at compile time, these are in conflict with previous definitions in your code of EV_msg_01 and station_msg_01.


    More generally, using bitfields for communication between hosts is a really bad idea. Different compilers lay out bitfields differently, which can cause miscommunication when sending them between hosts.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    Thanks Adak and grumpy. Actually this is what is required in our experiment, sending packets consisting of bits,which represents the messages.

    I will try to create functions instead of structs and pass the pointers to variables in the master struct.

    But again is there any other better way of doing the same thing.

    Thanks

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Sunjune View Post
    Thanks Adak and grumpy. Actually this is what is required in our experiment, sending packets consisting of bits,which represents the messages.
    All packets communicated over a network consist of bits. The problem with bitfields is that their order (within the struct containing them) is implementation defined. However, they are received over the network in the order they are sent.

    Quote Originally Posted by Sunjune View Post
    I will try to create functions instead of structs and pass the pointers to variables in the master struct.
    It is not possible to create a pointer to a bit field. Nor is it possible to create pointers to individual bits.
    Quote Originally Posted by Sunjune View Post
    But again is there any other better way of doing the same thing.
    You need to use a technique that ensures consistency of the order of bits sent over the network.

    Yes, you can use bitfields. But you need to use some logic so the sender place the various fields onto the network in some defined order, and the receiver retrieves those fields in the same order.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    Yes, you can use bitfields. But you need to use some logic so the sender place the various fields onto the network in some defined order, and the receiver retrieves those fields in the same order.
    So, if I make different struct for each different message and create an instance of them when there is a need to send that particular message, will that be better than the above method of mine. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers with Struct
    By JamesD in forum C Programming
    Replies: 12
    Last Post: 03-10-2011, 12:48 PM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Struct as pointers
    By wirefree101 in forum C Programming
    Replies: 8
    Last Post: 12-03-2009, 02:48 PM
  4. pointers to struct - help please
    By seminolas in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 01:03 AM
  5. Assigning pointers from pointers in struct.
    By Brian in forum C Programming
    Replies: 2
    Last Post: 10-18-2003, 04:30 AM

Tags for this Thread