Passing String to Function (Without Previous String Declaration)

This is a discussion on Passing String to Function (Without Previous String Declaration) within the C Programming forums, part of the General Programming Boards category; Is there a way to do this without declaring the strings "Line 1" or "Line 2" at the beginning of ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    26

    Passing String to Function (Without Previous String Declaration)

    Is there a way to do this without declaring the strings "Line 1" or "Line 2" at the beginning of main? (see below)

    The function call from main:
    Code:
    printLCD("Line 1","Line 2");
    function code:
    Code:
    void printLCD(const unsigned char* s_line1, const unsigned char* s_line2)
    {
        // Code for printing to LCD display here
    }

  2. #2
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Change your printLCD to

    Code:
    void printLCD(char* s_line1, char* s_line2)
    and it should work fine.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    26
    Thanks for the quick reply.

    I tried your suggestion, I still get a "type qualifier mismatch" warning message and what is printed out on the LCD screen is jumbled garbage.

    Also, I'm using the MPLAB C18 compiler.

    Appreciate the help as I'm a bit rusty on my C programming.

  4. #4
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    I'm not familiar with that compiler.

    I compiled this on VS and it works OK, see if it works for you.

    Code:
    void print(char *foo, char *bar)
    {
        printf("%s %s\n", foo, bar);
    }
    
    int main(void)
    {
        print("foo","bar");
        return 0;
    }

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by moddinati View Post
    I tried your suggestion, I still get a "type qualifier mismatch" warning message and what is printed out on the LCD screen is jumbled garbage.
    Are you sure this does not have to do with:

    Code:
       // Code for printing to LCD display here
    An unsigned char* is not quite the same thing as a (signed) char*, which might explain your type mismatch, altho without seeing what you are doing with it, it is hard to see how it would cause much of a problem.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Code:
    void printLCD(const char* s_line1, const char* s_line2)
    should work without any compiler warnings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a string to and from a function.
    By Obe in forum C Programming
    Replies: 10
    Last Post: 05-19-2010, 05:26 PM
  2. passing string to function
    By R.Stiltskin in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2009, 12:56 PM
  3. Passing string through a function...
    By MisterWonderful in forum C++ Programming
    Replies: 5
    Last Post: 04-15-2003, 02:33 PM
  4. Passing string to function
    By PotitKing in forum C Programming
    Replies: 8
    Last Post: 05-24-2002, 06:17 PM
  5. Passing string to function
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2001, 06:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21