Thread: How do I pass the sizeof a pointer array to a function

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

    How do I pass the sizeof a pointer array to a function

    Good morning...
    I have a header file containing:
    Code:
    char r_getPwr[TRANSMIT_SIZE] = {'E','R','_','C','M','D','#','P','?'};char r_getCommSpeed[TRANSMIT_SIZE] = {'E','R','_','C','M','D','#','U','?'};
    char r_getChan[TRANSMIT_SIZE] = {'E','R','_','C','M','D','#','C','?'};
    char r_getFreq[TRANSMIT_SIZE] = {'E','R','_','C','M','D','#','F','?'};
    char r_getVersion[TRANSMIT_SIZE] = {'E','R','_','C','M','D','#','T','3'};
    char r_getGrpID[TRANSMIT_SIZE + 1] = {'E','R','_','C','M','D','#','L','7','?'};
    char r_ack[3] = {'A','C','K'};
    
    
    struct s_rf{
        char command[10];
        boolean ack;
    };
    
    
    void rfTransmit(char* p);
    A source c file containing function defn:

    Code:
    void rfTransmit(char* c){
        char i;
        for ( i = 0; i < sizeof(c) ; i++) {
            UCA1TXBUF = *(c+i);
            while (!(UCA1IFG&UCTXIFG));
        }
    }
    And a source c file, main:
    Code:
    struct s_rf s_LPRS, *pyld;char *cmd;
    volatile char *prcv, noRcvChar = 0;
    
    
    int main(void)
    {
        WDTCTL = WDTPW | WDTHOLD;   // stop watchdog timer
        cmd = memcpy(s_LPRS.command, r_getVersion, sizeof(r_getVersion));
        s_LPRS.ack = T;
        pyld = &s_LPRS;
    
    
        IOconfig;
        initClockTo16MHz();
        initUART();
        while (1) {
            rfTransmit (cmd);
            __bis_SR_register(LPM3_bits + GIE); //go to sleep: LPM3
            if (noRcvChar == sizeof(s_LPRS.command) && (s_LPRS.ack == T)) {
                pyld = memcpy(s_LPRS.command, r_ack, sizeof(r_ack));
            }
        }
    
    
    }
    After the memcpy statement above in main I would like to know how to have the called function rfTransmit know the size of the cmd array. Can someone please tell me how to pass this into the function? When I do sizeof(c) within the function I get 2. This also confuses me somewhat as my compiler (CCS) recognizes char as 1 byte not 2??

    Thanks for all the help

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    928
    Apparently the size of a pointer on your device is 2 bytes. So sizeof(c), where c is a pointer to char, is 2. sizeof(*c), the size of what c points to, will be 1.

    As long as TRANSMIT_SIZE is at least 1 longer than the longest string (so we can have a zero byte terminator on all of them) then you can use the zero-terminator to know when you reach the end of the string (or even call strcpy in the called function).
    Code:
    char r_getPwr[TRANSMIT_SIZE]       = "ER_CMD#P?";
    char r_getCommSpeed[TRANSMIT_SIZE] = "ER_CMD#U?";
    char r_getChan[TRANSMIT_SIZE]      = "ER_CMD#C?";
    char r_getFreq[TRANSMIT_SIZE]      = "ER_CMD#F?";
    char r_getVersion[TRANSMIT_SIZE]   = "ER_CMD#T3";
    char r_getGrpID[TRANSMIT_SIZE + 1] = "ER_CMD#L7?";
    char r_ack[]                       = "ACK";        // let the size be set automatically (will be 4)
     
    void rfTransmit(char *s)
    {
        for (int i = 0; s[i]; i++)
        {
            UCA1TXBUF = s[i];
            while (!UCA1IFG&UCTXIFG)
                ;
        }
    }
     
    // Or even ...
    void rfTransmit(char *s)
    {
        while (*s)
        {
            UCA1TXBUF = *s++;
            while (!UCA1IFG&UCTXIFG)
                ;
        }
    }
    If you can't make the strings long enough to hold the zero-terminator then you would need to pass the size in as a second parameter.
    Last edited by john.c; 04-03-2020 at 08:36 AM.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    35
    Interesting that works quite nicely...I thought a for loop required conditionals such as i < s[i] but I guess not
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-20-2019, 02:00 PM
  2. How to pass pointer of 2d array to function
    By Alpo in forum C Programming
    Replies: 16
    Last Post: 04-25-2014, 05:21 PM
  3. pass the pointer of a two dimension array to a function
    By SoFarAway in forum C Programming
    Replies: 8
    Last Post: 04-13-2005, 05:43 AM
  4. sizeof() function on a pointer to an array of a struct
    By tegwin in forum C++ Programming
    Replies: 7
    Last Post: 05-30-2004, 02:30 PM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM

Tags for this Thread