How do I cast or promote a char variable to a char* variable? For
example, I want to use strcat to append a character to an existing
"string". (BTW, I'm not able to use STL string or CString data
types...) TIA 9 3799
"Michael R. Copeland" <mr*****@cox.net> wrote in message
news:MP************************@news.west.cox.net. .. How do I cast or promote a char variable to a char* variable? For example, I want to use strcat to append a character to an existing "string". (BTW, I'm not able to use STL string or CString data types...) TIA
char CharStr[20];
strcpy(CharStr, "Test";
char SomeChar = 'A';
char TempStr[2];
TempStr[0] = SomeChar;
TempStr[1] = '\0';
strcat( CharStr, TempStr );
That's one way.
> How do I cast or promote a char variable to a char* variable? For example, I want to use strcat to append a character to an existing "string". (BTW, I'm not able to use STL string or CString data types...) TIA
A char is a char. A char* often is either a pointer to a single char or,
much more often, a pointer to a zero-terminated char array (AKA a C-style
string).
And so, let's bear in mind that string functions such as concat operate on
strings not chars.
You can have a string that contains only one character (not counting the
terminating zero, if any).
You should be able to use std::string, like so:
#include <string>
#include <iostream>
int main()
{
using std::string;
using std::cout;
string str = "a";
char c = 'b';
str += c;
cout << str; // outputs "ab"
}
Regards,
Ben
The thing is, you can't realy.
If you have
char ch = 'a';
the only the you can do to cast it to char* is to copy it to a 2 byte
array as described below...
"Michael R. Copeland" <mr*****@cox.net> wrote in message
news:MP************************@news.west.cox.net. .. How do I cast or promote a char variable to a char* variable? For example, I want to use strcat to append a character to an existing "string". (BTW, I'm not able to use STL string or CString data types...) TIA
In addition ot the other comments, it seems very dangerous to cast a char to
a char *; the size of the char * is generally larger than the size of char.
You can do something like this:
char a = 'a';
char *b = &a;
However, if you wanted to use strcat, b is not null terminated and would
likely cause strcat to malfunction.
If a function such as strncat is available to you, you could do something
like
strncat(dest, b, 1);
which appends on character from 'b' to dest.
> The thing is, you can't realy. If you have char ch = 'a'; the only the you can do to cast it to char* is to copy it to a 2 byte array as described below...
I don't see anything "below". That's my question - how _do_ I make
the cast so I can strcat (or wharever) the character?
"Michael R. Copeland" wrote: The thing is, you can't realy. If you have char ch = 'a'; the only the you can do to cast it to char* is to copy it to a 2 byte array as described below...
I don't see anything "below". That's my question - how _do_ I make the cast so I can strcat (or wharever) the character?
As has been pointed out: You can't. Well, technically you can, but it doesn't do
you no good. strcat doesn't expect just a pointer. strcat expects a pointer
to an array and that array must be 0-terminted. A single character isn't 0
terminated, thus strcat will do fancy things.
All you need to do is set up a 2 element array, copy your character into
the first position, make sure the C-style string is 0-terminated and there
you go: strcat() does the right thing:
char ch = 'a';
char tmp[2];
tmp[0] = ch;
tmp[1] = '\0';
strcat( to_whatever, tmp );
--
Karl Heinz Buchegger kb******@gascad.at
"Michael R. Copeland" <mr*****@cox.net> schrieb im Newsbeitrag
news:MP************************@news.west.cox.net. .. How do I cast or promote a char variable to a char* variable? For example, I want to use strcat to append a character to an existing "string". (BTW, I'm not able to use STL string or CString data types...) TIA
char some[128]="";
char add='a';
strcat(some, (char*)&((short)add<<8));
Gernot Frisch wrote: "Michael R. Copeland" <mr*****@cox.net> schrieb im Newsbeitrag news:MP************************@news.west.cox.net. .. How do I cast or promote a char variable to a char* variable? For example, I want to use strcat to append a character to an existing "string". (BTW, I'm not able to use STL string or CString data types...) TIA
char some[128]=""; char add='a';
strcat(some, (char*)&((short)add<<8));
To say it with the words of my compiler:
error C2102: '&' requires l-value
--
Karl Heinz Buchegger kb******@gascad.at
"Karl Heinz Buchegger" wrote: Gernot Frisch wrote: "Michael R. Copeland" schrieb: How do I cast or promote a char variable to a char* variable? For example, I want to use strcat to append a character to an existing "string". (BTW, I'm not able to use STL string or CString data types...) TIA
char some[128]=""; char add='a';
strcat(some, (char*)&((short)add<<8));
To say it with the words of my compiler: error C2102: '&' requires l-value
Even if one changes the code so that the construct is an
l-value and will compile, it still won't work on a little-endian
system (such as windows), in which 0x6100 looks like "0x0061"
in memory, which, as a string, is "\0a", which is the empty string.
It works if you get rid of the shift:
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
int main()
{
char Text[128] = "fjwux";
cout << "Original string = " << Text << endl;
short Add = (static_cast<short>('a')); // 0x0061, little-endian 0x6100
strcat(Text, reinterpret_cast<char*>(&Add)); // concatenates "a/0"
cout << "Add = " << std::hex << std::showbase << Add << endl;
cout << "Combined string = " << Text << endl;
return 0;
}
That prints:
Original string = fjwux
Add = 0x61
Combined string = fjwuxa
It works (at least, on little-endian systems), but it's very
messy and brittle.
Much better is:
std::string Text ("fjwux");
Text += 'a';
cout << Text << endl;
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Brian Blais |
last post by:
Hello,
I saw on a couple of recent posts people saying that casting the return
value of malloc is bad, like:
d=(double *) malloc(50*sizeof(double));
why is this bad? I had always thought...
|
by: Ramesh Tharma |
last post by:
Hi,
Is any one knows what's wrong with the following code, I was told that it
will compile and run but it will crash for some values.
Assume that variables are initilized.
char* c;
long*...
|
by: mr_semantics |
last post by:
I have been reading about the practise of casting values to unsigned
char while using the <ctype.h> functions. For example,
c = toupper ((unsigned char) c);
Now I understand that the standard...
|
by: Alex Vinokur |
last post by:
classes that have virtual methods hold pointer to virtual table as
additional implicit data member. So, sizeof of such classes is sizeof
of all data (as struct-POD) plus 4.
It seems that use of...
|
by: brekehan |
last post by:
I've always been a little sketchy on the differences between static,
dynamic, and reinterpret casting. I am looking to clean up the
following block by using C++ casting instead of the C style...
| |
by: Francine.Neary |
last post by:
I've read that you should always cast the argument you pass to
isupper(), isalnum(), etc. to unsigned char, even though their
signature is int is...(int).
This confuses me, for the following...
|
by: sophia.agnes |
last post by:
Hi ,
I was going through peter van der linden's book Expert C programming,
in this book there is a section named "How and why to cast"
the author then says as follows
(float) 3 - it's a...
|
by: alex.j.k2 |
last post by:
Hello all,
I have "PRECISION" defined in the preprocessor code
and it could be int, float or double, but I do not know in the
code what it is.
Now if I want to assign zero to a "PRECISION"...
|
by: Tinkertim |
last post by:
Hi,
I have often wondered if casting the return value of malloc() (or
friends) actually helps anything, recent threads here suggest that it
does not .. so I hope to find out.
For instance :
...
|
by: Alex Vinokur |
last post by:
Hi,
Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?
Alex Vinokur
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |