Thread: Parameter problem

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    147

    Parameter problem

    er.........
    i have a function which accepts char[]

    and i want to pass in a string

    there's no problem with passing in :

    function ("HELLO");

    but i'm facing a problem when i want to pass in a string which also contains some special code character which i dunno how to represent, all i know is that i usually use this character like this :

    printf ("%c", 186);

    so what i want to pass into this function is something with a combination of "\tHELLO" and this '186' character

    i dunno how i can achieve this as a parameter, can someone help me out?
    Only by the cross are you saved...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well you're close with what you have. Add an 's' to that printf call first.

    sprintf( buffer, format, ... );

    Such as:
    Code:
    
    sprintf( buf, "%s%c%c", "hello", 186, 129 );
    myfun( buf );
    [edit]
    On a side note, 186 is greater than what a signed character uses. You're trying for extended characters, I suspect. You really should be using some other method. Perhaps using an array of unsigned characters instead.
    [/edit]

    Quzah.
    Last edited by quzah; 07-07-2003 at 10:06 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    i see, but wat do i put under buffer?

    i onli see the need for the 2nd parameter, can i not create a buffer?
    Only by the cross are you saved...

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by fkheng
    i see, but wat do i put under buffer?

    i onli see the need for the 2nd parameter, can i not create a buffer?
    The common implementation here is to use an array as your buffer. However, as described, you'll have issues with your value you're trying to use, since it falls outside of the bounds of a signed character.

    However, here is an example:
    Code:
    char buf[10] = {0};
    sprintf( buf, "hello%c", '!' );
    printf( "Buf contains: '%s'\n", buf );
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Some compilers support entering non-ascii character directly into text strings by using the \x modifier. For example:

    strcpy (buffer, "HELLO\xff");
    and
    sprintf (buffer,"HELLO%c", 255);

    Is the same as:

    sprintf (buffer, "%s%c", "HELLO", 255);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM