Thread: passing struct containing char string into a function

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

    passing struct containing char string into a function

    I have a header file containing:
    Code:
    struct confTimer{
        char name[3];
        char direction[8];
        uint countValue;
    };
    
    
    uint run_timer(struct confTimer cT);
    I have a function file containing:
    Code:
    uint run_timer(struct confTimer cT){
        if (!strcmp(cT.name, "B0")) {
    I have main (separate file) containing:
    Code:
        struct confTimer radioT;
    
    
    
    
    	radioT.name[3] = "B0";
    	radioT.direction[8] = "up_down";
    	radioT.countValue = 4455;
            run_timer(radioT);
    I am never stepping into the function at the conditional if...What am I missing?
    Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You can not assign c-string in C!
    You need to use strcpy or strncpy function to copy C-strings.

    I suggest reading the warning you get while compiling!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    radioT.name[3] = "B0";
    And, this is wrong even in C++!

    In C++, this should work.

    Code:
    radioT.name = "B0";
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    You should've got warnings with that code. Don't ignore warnings.
    You need to use strcpy to set the contents of a string.
    And you should probably pass the struct by pointer.
    Code:
    #include <stdio.h>
    #include <string.h>
     
    typedef unsigned int uint;
     
    struct confTimer{
        char name[3];
        char direction[8];
        uint countValue;
    };
     
    uint run_timer(struct confTimer *cT){
        if (!strcmp(cT->name, "B0")) {
            printf("%s, %d\n", cT->direction, cT->countValue);
        }
        return 0;
    }
     
    int main() {
        struct confTimer radioT;
        strcpy(radioT.name, "B0");
        strcpy(radioT.direction, "up_down");
        radioT.countValue = 4455;
        run_timer(&radioT);
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to initialise, you can do so with an aggregate:
    Code:
    struct confTimer radioT = {"B0", "up_down", 4455};
    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. Passing Typedef Struct to a function
    By serge in forum C Programming
    Replies: 5
    Last Post: 03-28-2020, 10:51 AM
  2. Replies: 7
    Last Post: 03-04-2012, 11:17 AM
  3. passing struct to function
    By silentkarma in forum Windows Programming
    Replies: 15
    Last Post: 03-07-2008, 12:57 AM
  4. passing struct to function help
    By staticalloc in forum C Programming
    Replies: 4
    Last Post: 10-06-2004, 08:30 AM
  5. passing struct to function
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2002, 12:52 AM

Tags for this Thread