Thread: converting a char to a "string"

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    converting a char to a "string"

    I have a function that accepts a c-string (a char *). The function transmits each character of the string via the serial port. Inside the function, the strlen function is used to determine how many characters to transmit. The function works well when I do...

    Code:
    void Transmit_String(char *Log_String) {
      int Line_Ready = 0;
      int Length = strlen(Log_String);
      int x;
    
      for (x = 0; x < Length; x++) {
        // Check to make sure port is ready to send
        while (Line_Ready == 0) {
          Line_Ready = inportb(COM2_LINE_STATUS) & 0x20;
        }
        Line_Ready = 0;
        // Send next byte
        outportb(COM2_BUFFERS, Log_String[x]);
      }
    }
    
    // Example Call
    Transmit_String("Hello World!");
    However, I'd like to send a single character that I get from a variable...

    Code:
    char Manual_Command = '~';
    
    //I initially tried
    Transmit_String(&Manual_Command);
    
    //Then...
      char *Temp = (char *) malloc(sizeof(char) * 5);
    
        sscanf(Temp, "%c", &Manual_Command);
        Transmit_String(Temp);
    I always seem to get random junk though instead of the expected character.

    What am I doing wrong?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You either need a separate function to send one character, or make it into a string (an array of 2 characters, with the second character a zero).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Doesn't my second attempt make it into a string? The sscanf?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Bladactania View Post
    Doesn't my second attempt make it into a string? The sscanf?
    Maybe you wanted to use sprintf instead of sscanf? That would make more sense there...the sscanf will be reading from Temp into ManualCommand (why?). Then you send Temp, which has no defined content.
    Last edited by MK27; 04-20-2009 at 01:03 PM.
    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

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Oh my good Lord! That's what I get for reading forums before coding! I had been reading a thread about sscanf.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. Replies: 6
    Last Post: 06-30-2005, 08:03 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM

Tags for this Thread