Thread: Basic question how to pass a char string to a function?

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

    Basic question how to pass a char string to a function?

    I have two files main.c and lprs_mspConfig.c
    I want to pass a string argument from main to a function can someone tell me how to do this.

    Main.c
    Code:
    int main(void){
    
    
        WDTCTL = WDTPW | WDTHOLD;    // stop watchdog timer
        
        config_timer('B0', 2344);
    
    
        LPM3;
        return 0;
    }
    lprs_mspConfig.c
    Code:
    void config_timer(char name[3], uint v){
        char *pName;
        pName = name;
    
    
        if (*pName == 'B0') {
            TB0CCR0 = v;
            TB0CTL = TBSSEL__ACLK;
            TB0CCTL0 &= ~CCIFG;
    
    
        }
        else if (*pName == 'B1') {
            TB1CCR0 = v;
            TB1CTL = TBSSEL__ACLK;
            TB1CCTL0 &= ~CCIFG;
        }
        else if (*pName == 'B2') {
            TB2CCR0 = v;
            TB2CTL = TBSSEL__ACLK;
            TB2CCTL0 &= ~CCIFG;
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,076
    You might want to recall that string literals are delimited by double quotes, not single quotes.
    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
    50
    I tried double quotes....I can see name = "B0" coming into function....
    but name == "B0" conditional never seems to get met

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,076
    Quote Originally Posted by ridgerunnersjw
    but name == "B0" conditional never seems to get met
    That's because you end up comparing pointers rather than the string content. Use strcmp instead, or take advantage of the characteristics of the expected string data (it always starts with a 'B' followed by '0', '1', or '2') to write the if statements.

    Note that you don't need pName as you can use name directly. So you might do something like this:
    Code:
    void config_timer(const char name[], uint v) {
        if (name[0] == 'B' && name[2] == '\0') {
            switch (name[1]) {
            case '0':
                TB0CCR0 = v;
                TB0CTL = TBSSEL__ACLK;
                TB0CCTL0 &= ~CCIFG;
                break;
            case '1':
                TB1CCR0 = v;
                TB1CTL = TBSSEL__ACLK;
                TB1CCTL0 &= ~CCIFG;
                break;
            case '2':
                TB2CCR0 = v;
                TB2CTL = TBSSEL__ACLK;
                TB2CCTL0 &= ~CCIFG;
                break;
            }
        }
    }
    I've changed name from char[3] to const char[] because the 3 isn't enforced anyway and the const makes it clear that you won't modify the string content so it is safe to pass a string literal as an argument. You may also find it useful to have a return value to indicate success/failure or a status/error code.
    Last edited by laserlight; 3 Weeks Ago at 04:33 PM.
    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
    50
    Thank you....
    That worked....I didn't realize strcmp returns a 0 when true....

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    746
    Quote Originally Posted by ridgerunnersjw View Post
    That worked....I didn't realize strcmp returns a 0 when true....
    strcmp doesn't return a "true" or "false" value. It returns a value less than 0, 0, or a value greater than 0, depending on the relative order of the string arguments.

    You can think of it as returning the "difference" between the strings (like a - b), where a value less than 0 tells you that string a is "less than" string b.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-20-2016, 03:13 AM
  2. How to pass a string to a function
    By valentin in forum C++ Programming
    Replies: 6
    Last Post: 09-13-2013, 02:28 PM
  3. Pass an string of array in a function
    By kantida in forum C Programming
    Replies: 1
    Last Post: 04-29-2011, 04:50 AM
  4. Replies: 1
    Last Post: 11-16-2008, 03:46 PM
  5. Pass a string to a function
    By colinuk in forum C Programming
    Replies: 10
    Last Post: 01-31-2005, 08:05 PM

Tags for this Thread