Casting char to char*

  • Thread starter Michael R. Copeland
  • Start date
M

Michael R. Copeland

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
 
J

Josh Mcfarlane

Michael said:
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

And why do you want to do this? Can you provide an example of what you
are trying to do? This smells of homework.
 
M

Michael R. Copeland

How do I cast or promote a char variable to a char* variable? For
And why do you want to do this? Can you provide an example of what you
are trying to do? This smells of homework.

It's absolutely NOT homework - I've been around UseNet & FidoNet far
too long to that sort of abuse.
That said, here's what I need to do:

char sEvents[26] = ""; // an empty string
....
I wish to append some individual characters to this "string",
depending on various conditions, so that ultimately it might have a
value of "abdemnst". Such a problem could be done in a loop ('a'-'z'),
where some condition directs the placement of a character (or not).
Yes, there are fancier tools (bitset, etc.) that I could use, but
there are application reasons that prevent my reengineering and
reimplementing a lot of legacy code at this time - I'm working with
hundreds of thousands of lines of code, and I really can't rip apart its
data structures at this time.
Thus, I just want to know how to append individual characters to
existing "strings"...
 
J

John Harrison

Michael said:
And why do you want to do this? Can you provide an example of what you
are trying to do? This smells of homework.


It's absolutely NOT homework - I've been around UseNet & FidoNet far
too long to that sort of abuse.
That said, here's what I need to do:

char sEvents[26] = ""; // an empty string
...
I wish to append some individual characters to this "string",
depending on various conditions, so that ultimately it might have a
value of "abdemnst". Such a problem could be done in a loop ('a'-'z'),
where some condition directs the placement of a character (or not).
Yes, there are fancier tools (bitset, etc.) that I could use, but
there are application reasons that prevent my reengineering and
reimplementing a lot of legacy code at this time - I'm working with
hundreds of thousands of lines of code, and I really can't rip apart its
data structures at this time.
Thus, I just want to know how to append individual characters to
existing "strings"...

What didn't you like about Jim Langston's answer? It is the correct way
to do it.

john
 
M

Michael R. Copeland

Thus, I just want to know how to append individual characters to
What didn't you like about Jim Langston's answer? It is the correct way
to do it.

Formatting a string variable (with multiple statements) for use in
the strcat seems like a lot of extra code - I was hoping there was some
sort of cast or "promotion" that would work. Something like:

strcat(string_var, (char*)char_var); /* which won't work */

It's disappointing that such a simple process is so cumbersome and
difficult to code...
 
K

Karl Heinz Buchegger

Michael R. Copeland said:
Formatting a string variable (with multiple statements) for use in
the strcat seems like a lot of extra code - I was hoping there was some
sort of cast or "promotion" that would work.

One more time.
strcat has some requirements on what you feed to it.
Beeing a character pointer is not enough.
Each pointer has to point to a sequence of characters and each of those
sequences has to be 0-terminated.

Such are the rules for using strcat.

Casting is of no help, since casting doesn't change the character. It just
tells the compiler: "Look, here is a character and I want you to look at it
as if it were something else."

But just by telling to look at it in a different way, things don't change
magically. It is still a single, lonesome character. You need to *convert*
that character into something strcat can work with.


Something like:
strcat(string_var, (char*)char_var); /* which won't work */

It's disappointing that such a simple process is so cumbersome and
difficult to code...

Difficult ?????
There is nothing difficult about it.

Just wrap it up in another function and be done. What do you think strcat()
is, magic? It is a function too! The only difference is that somebody already
programmed that function for you, since catanating 2 strings is a common operation.

char* strccat( char* dest, char src )
{
char Tmp[2];
Tmp[0] = src;
Tmp[1] = '\0';

strcat( dest, Tmp );

return dest;
}

Once you have that, simply use it. You do the same with strcat (simply use a function),
so why not write a strccat that exactly does what you want?

And since we are in comp.lang.c++ the question must be permitted:
Why do you deal with those error prone C-style character strings. Why don't
you simply use the C++ tool for all of this: std::string?
It does exactly what you want (and much more) out of the box.
 
B

Ben Pope

Michael said:
Formatting a string variable (with multiple statements) for use in
the strcat seems like a lot of extra code - I was hoping there was some
sort of cast or "promotion" that would work. Something like:

strcat(string_var, (char*)char_var); /* which won't work */

It's disappointing that such a simple process is so cumbersome and
difficult to code...

Yes, but thats the point. If you use C style constructs, you don't get
all the additional "magic" of the extra work being done for you.

If you had used std:strings from the beginning they provide a mechanism
for adding a char to a string as part of the library. If the C-style
string is not part of your code, then that doesn't prevent you from
writing your code with std::strings as has already been demonstrated.

strcat does not provide the functionality to add a char to a string, but
as has already been said, you can write a function to do it.

Presumably you could even provide an overload of strcat (put it in your
own namespace) that takes a char as the second argument and pretend that
it was part of strcat in the first place. Chances are that once the
compiler gets hold of it, it'll inline the function calls and optimise
any overhead away.

Ben
 
D

Default User

Karl Heinz Buchegger wrote:

Just wrap it up in another function and be done.
char* strccat( char* dest, char src )


Pick a different name though, all names with external linkage that
begin with "str" and a lowercase letter are reserved.



Brian
 
D

Dave Rahardja

Karl Heinz Buchegger wrote:





Pick a different name though, all names with external linkage that
begin with "str" and a lowercase letter are reserved.

Only in the std namespace if you do it right.

-dr
 
P

peter.koch.larsen

Default said:
Karl Heinz Buchegger wrote:





Pick a different name though, all names with external linkage that
begin with "str" and a lowercase letter are reserved.



Brian

That might be the case for C. For C++ there are no such restrictions.

/Peter
 
D

Default User

That might be the case for C. For C++ there are no such restrictions.

I thought there was a clause that names reserved for future C library
directions were also reserved in C++, but I don't find that in my check
of the Standard.



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top