Thread: function parameters

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    function parameters

    It has been a while since I have been working with details on specifically appropriately passing parameters into a function. Basically, I have a snippet of code as shown below:

    Code:
    #define BUFFER_SIZE 5
    
    void main(){
           unsigned int temp_res;
           char adc[BUFFER_SIZE];
    
           void usart_str_write(char* string);
           char Usart_write(char data);         // given by built-in library
    
           /* some register assignment commands that's not relevant to the problem */
    
        while(1) {
               temp_res = Adc_Read(0);
               n = sprintf(adc, "%u", temp_res);
               if (n < 0 || n >= BUFFER_SIZE)
                    break;
               usart_str_write(adc);
        }
    }
    
    void usart_str_write(char* string)
    {
    	while (null character hasn't been reached)
    	{
    		Usart_write(string);
    		string++;
    	}
    }
    I'm not sure if I should have my usart_str_write as "void usart_str_write(char* string)" to pass adc[] by reference or by "void usart_str_write(char string[]". Does anyone have an insight on this?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Main should return int. See FAQ.
    Don't put declarations inside your functions; they should go into a header file.
    It's up to you, really, how to pass them. If you do char string[], the entire string is duplicated and passed to the function, but if you use (const) char* string, it will pass the address, which is faster (but also allows it to modify your string). In any case, char* and const char* is standard for all (AFAIK) C library functions.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    My code runs on microcontrollers, which is correct to use void main(). I don't want to pass char string[] b/c I don't have the luxury of having the duplication take up more memory. If I pass the address, does the function call automatically dereference string to the address of adc[]?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No. If you pass a pointer, you pass a pointer, end of story. You're going to have to derference it yourself.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    So are you suggesting I have something like:

    Code:
    #define BUFFER_SIZE 5
    
    void usart_str_write(char* string);
    char Usart_write(char data);         // given by built-in library
    
    void main(){
           unsigned int temp_res;
           char adc[BUFFER_SIZE];
           char* adc_str;
    
           /* some register assignment commands that's not relevant to the problem */
    
        while(1) {
               temp_res = Adc_Read(0);
               n = sprintf(adc, "&#37;u", temp_res);
               if (n < 0 || n >= BUFFER_SIZE)
                    break;
               adc_str = &adc;
               usart_str_write(adc_str);
        }
    }
    
    void usart_str_write(char* string)
    {
    	while (null character hasn't been reached)
    	{
    		Usart_write(string);
    		string++;
    	}
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by stanlvw View Post
    So are you suggesting I have something like:

    Code:
    #define BUFFER_SIZE 5
    
    void usart_str_write(const char* string);
    char Usart_write(char data);         // given by built-in library
    
    void main(){
           unsigned int temp_res;
           char adc[BUFFER_SIZE];
           char* adc_str; /* Remove */
    
           /* some register assignment commands that's not relevant to the problem */
    
        while(1) {
               temp_res = Adc_Read(0);
               n = sprintf(adc, "%u", temp_res);
               if (n < 0 || n >= BUFFER_SIZE)
                    break;
               adc_str = &adc; /* Remove */
               usart_str_write(adc);
        }
    }
    
    void usart_str_write(const char* string)
    {
    	while (null character hasn't been reached)
    	{
    		Usart_write(string);
    		string++;
    	}
    }
    You don't need an extra var for getting the address.
    Further, on (char) arrays, you don't need to do & to get the address. It's done automatically. So just pass adc.
    Also add const to your function arguments unless you're going to modify the string (I assume you're not?).

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I would not be modifying thstring in usart_str_write. Thanks for the heads up on the const for prevent unintended modifications.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No problem.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I have another problem with the compiler complaining about:

    Code:
    usart_str_write(adc[]);
    I have tried both adc and adc[], but the compiler complained about the same message about it being an illegal expression and implicit cast of integral value to pointer. Any ideas on what's happening that's causing this complaint?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What's the code and what line does the error occour on?

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you're doing pointer arithmetic with a variable that's not a pointer.

    edit: ack: I thought you were doing this:
    Code:
    void usart_str_write(const char string[])
    Last edited by robwhit; 11-12-2007 at 12:13 AM.

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Code:
    #define BUFFER_SIZE 5
    
    void usart_str_write(const char* string);
    char Usart_write(char data);         // given by built-in library
    
    void main(){
           unsigned int temp_res;
           char adc[BUFFER_SIZE];
           char* adc_str; /* Remove */
    
           /* some register assignment commands that's not relevant to the problem */
    
        while(1) {
               temp_res = Adc_Read(0);
               n = sprintf(adc, "%u", temp_res);
               if (n < 0 || n >= BUFFER_SIZE)
                    break;
               adc_str = &adc; /* Remove */
               usart_str_write(adc);
        }
    }
    
    void usart_str_write(const char* string)
    {
    	while (null character hasn't been reached)
    	{
    		Usart_write(string);
    		string++;
    	}
    }
    The error is on the line highlighted in red. The message is shown in http://imagebin.ca/view/89NYVWS.html.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I dunno, that should work, as it works for me.
    Also,
    Code:
    Usart_write(string);
    ...should be...
    Code:
    Usart_write(*string);
    Since it takes a char.

  14. #14
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Elysia: It works on the compiler that you're using?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it does.
    It compiles fine on VC++ 2005.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM