Thread: passing char where array of chars required

  1. #1
    larry
    Guest

    passing char where array of chars required

    What if function requires a pointer to first item in char array (like "void function(char* text)") and I need to pass one character - single variable of type char. Do I always have to create an array, set its first character to the character from the variable and create a pointer to it, or is there any other, smarter, way to do this?

  2. #2
    V
    Guest
    Well, I'm assuming you're using this because you want to pass a character to a text function.

    The answer is yes, you must pass an array. The reason is, the text functions take the address of the first character -- so they don't know when a string exds, except by the addition of the extra character -- the NUL character -- at the end of the string. So every string with one "real" character is really 2 chars long.

    It's not that hard to do what you want, you can just do something like this (assume theChar is already initialized to the char you want):


    Code:
    char buff[2];
    sprintf((char *) &buff,"%c",theChar);
    function((char *) &buff);
    
    Of course, this is exactly equivalent to:
    
    char buff[2];
    buff[0] = theChar;
    buff[1] = 0;
    function((char *) &buff);
    You can pass the address of a single character SOMETIMES, but *only* if:

    1) One of the parameters specifies the number of characters to operate on.
    2) You know that the byte of memory immediately following your character is zeroed.

    Case 1) happens if you use commands that work on parts of a string, and case 2) happens in a few ways, but the easiest is if your char is part of a struct or object -- then you know what variable immediately follows it in memory, and if you know this is zero, you can use it.

    Even in these cases, however, you may simply want to allocate the buffer and do the above -- it's fastest and safest.

  3. #3
    larry
    Guest
    thankx

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void function(char* text)
    If you're passing literal characters, and you wanted to do something like
      function( 'a' ); // single quotes

    What you would need to write is this
      function( "a" ); // double quotes
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    I know this, but the problem is that I don't know the character, I'm getting it from user. This takes me to solution! I'd create string variable for the user input and put it in it. So there's no need of extra variable for function "void function(char* text)". Nice. Thanx again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM