将char转换为char * [英] Casting char to char*

查看:184
本文介绍了将char转换为char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将char变量转换或提升为char *变量?对于

示例,我想使用strcat将字符附加到现有的

" string"。 (顺便说一句,我不能使用STL字符串或CString数据

类型......)TIA

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

推荐答案

Michael R. Copeland <先生***** @ cox.net>在消息中写道

新闻:MP ************************ @ news.west.cox.net。 ..
"Michael R. Copeland" <mr*****@cox.net> wrote in message
news:MP************************@news.west.cox.net. ..
如何将char变量转换或提升为char *变量?对于
示例,我想使用strcat将字符附加到现有的
string。 (顺便说一句,我不能使用STL字符串或CString数据类型......)TIA
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);


这是单向的。



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.


>如何投射或提升char变量to char * variable?对于
> How do I cast or promote a char variable to a char* variable? For
例子,我想使用strcat将一个字符附加到现有的
string中。(顺便说一句,我不能使用STL)字符串或CString数据
类型...)TIA
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是char。一个char *通常是指向单个char的指针,或者

更常见的是指向零终止字符数组的指针(AKA是C风格的

字符串)。


所以,让我们记住字符串f像concat这样的部分操作

字符串而不是字符。


你可以拥有一个只包含一个字符的字符串(不包括
$ b) $ b终止零,如果有的话。


您应该能够使用std :: string,如下所示:


#include< ; string>

#include< iostream>


int main()

{

使用std :: string;

使用std :: cout;


string str =" a" ;;

char c =''''';


str + = c;

cout<<海峡; //输出" ab"

}


问候,

Ben



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

事情是,你不能真的。

如果你有

char ch =''a'';

你唯一能把它投射到char *就是把它复制到一个2字节的
阵列,如下所述...

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...


这篇关于将char转换为char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆