C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-20-2009, 12:44 PM   #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?
Bladactania is offline   Reply With Quote
Old 04-20-2009, 12:53 PM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 04-20-2009, 12:55 PM   #3
Registered User
 
Join Date: Feb 2009
Posts: 278
Doesn't my second attempt make it into a string? The sscanf?
Bladactania is offline   Reply With Quote
Old 04-20-2009, 01:01 PM   #4
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS

Last edited by MK27; 04-20-2009 at 01:03 PM.
MK27 is offline   Reply With Quote
Old 04-20-2009, 01:03 PM   #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
Bladactania is offline   Reply With Quote
Reply

Tags
char, malloc, sscanf, string

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting bits with Unions.... Not working; please help!! dan56965 C Programming 12 08-11-2008 11:02 PM
Obtaining source & destination IP,details of ICMP Header & each of field of it ??? cromologic Networking/Device Communication 1 04-29-2006 02:49 PM
program sections were working fine until it came to putting it together shoobsie C Programming 6 06-30-2005 08:03 AM
Program Crashing Pressure C Programming 3 04-18-2005 10:28 PM
comparing fields in a text file darfader C Programming 9 08-22-2003 08:21 AM


All times are GMT -6. The time now is 11:55 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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