Thread: help using extern struct in c??

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    35

    help using extern struct in c??

    Hello....
    I am using C89 compiler (CCS) and have an h file containing
    Code:
    extern struct s_rf{    char command[10];
        boolean ack;
    }s_radioMssg;
    In main I then try and initialize
    Code:
    int main(void){
        WDTCTL = WDTPW | WDTHOLD;   // stop watchdog timer
        s_radioMssg.command = {'F'};
        s_radioMssg.ack = T;
    I am not sure but I was thinking that in main I am defining the structure components. I have tried even adding before main

    Code:
    struct s_rf s_radioMssg;
    in the event that the header file was ONLY declaring s_radioMssg as an s_rf struct but that seems redundant to me to define before main??

    The compiler errors as follows:
    #138 expression must be a modifiable lvalue

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,719
    The problem you're having is this: the member named command is an array, and you cannot assign to an array because it is not "a modifiable lvalue". You can assign to the elements of this array instead, e.g.,
    Code:
    s_radioMssg.command[0] = 'F';
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    35
    So there is no way to
    Code:
    s_radioMssg.command[] = {'F'}
    such that all values get initialized at once??

    Also is there a better way to declare, define, instantiate a structure such as the one I have? I figured I'd declare it in .h file (maybe I shouldn't be using extern here??), then define it in main and finally instantiate/initialize it in main and or other c files?

    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,719
    Is s_radioMssg really supposed to be a global variable, i.e., does it represent truly global state?

    If not, then I suggest that in your header:
    Code:
    struct s_rf {
        char command[10];
        boolean ack;
    };
    Then in a source file:
    Code:
    int main(void) {
        WDTCTL = WDTPW | WDTHOLD;   // stop watchdog timer
        struct s_rf s_radioMssg = {{'F'}, T};
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2020
    Posts
    35
    What you posted works but only for [0] of the array....after reading online I found that memcpy seems to work quite well....Ultimately I wanted to initialize the array but then I wanted to update the s_radioMssg.command = r_getFreq where r_getFreq is a char array

    Thank you for your help

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,719
    Quote Originally Posted by ridgerunnersjw
    What you posted works but only for [0] of the array
    No, it works for the entire array:
    Code:
    struct s_rf s_radioMssg = {{'F', '1', '2', '3', '4', '5', '6', '7', '8', '9'}, T};
    Quote Originally Posted by ridgerunnersjw
    after reading online I found that memcpy seems to work quite well
    It doesn't: your array does not store a null terminated string.

    Quote Originally Posted by ridgerunnersjw
    Ultimately I wanted to initialize the array but then I wanted to update the s_radioMssg.command = r_getFreq where r_getFreq is a char array
    Use a loop to copy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get Extern struct to work properly ?
    By pixie_laluna in forum C Programming
    Replies: 2
    Last Post: 03-03-2019, 08:58 PM
  2. struct and extern
    By jgtech in forum C Programming
    Replies: 1
    Last Post: 04-12-2011, 05:39 AM
  3. cannot compile with extern struct
    By jeanluca in forum C Programming
    Replies: 4
    Last Post: 06-08-2009, 02:51 AM
  4. extern struct
    By beginner.c in forum C Programming
    Replies: 10
    Last Post: 07-01-2007, 07:43 PM
  5. extern struct...
    By Supar in forum C++ Programming
    Replies: 7
    Last Post: 01-07-2003, 02:58 PM

Tags for this Thread