将char *数组转换为C ++中的字符引用 [英] convert char* array to character reference in C++

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

问题描述

大家好,



我想在不使用C ++标准字符串库的情况下将char *数组转换为字符引用。

<例如:
例如:



Hi guys,

I want to convert char* array to character reference without using the standard String library of C++.

for example:

char* CharacterConstant;
convertedstring = "";
char& characterReference;
char = CharacterConstant;



C ++中是否存在类似编码选项的可能性?



提前致谢!


Is there a possibility of similar coding option in C++?

Thanks in advance!

推荐答案

问题尚不清楚。但是如果你想从指针初始化一个引用,只需传递内容:

The question is unclear. But if you want to initialize a reference from a pointer, just pass the content:
const char *ConstString = "test";
const char& ConstRef = *ConstString;

char NonConstString[] = "test";
char& Ref = *NonConstString;


C / C ++中的术语引用指的是一种特殊的类型限定符,它不能像你看起来那样理性地使用打算。而且,没有限定符的类型 char 只定义了一个字符,而不是字符串!



您可以将指向 char 的指针转换为引用,例如这个:

The term ''reference'' in C/C++ refers to a specific type qualifier that cannot be used sensically in the way you seem to intend. Moreover, the type char, without qualifiers, defines just a single character, not a string!

You can convert a pointer to a single char to a reference, like this:
char* my_string_pointer = new char[10]; // points to a char array
my_string_pointer[0] = 'a';
my_string_pointer[1] = 'b';
my_string_pointer[2] = 'c';
char* my_char_pointer = my_string_pointer + 1; // points to 'b'
const char& my_char_reference = *my_char_pointer; // reference to 'b'
char my_char = *my_char_pointer; // only a copy of 'b'
my_char = 'd'; // now my_char is 'd', but *my_char_pointer is still 'b'!
my_char_reference = 'e'; // now *my_char_pointer is 'e'





请注意,C / C ++中的引用总是 const ,尽管如果省略,很多编译器都会接受const 限定符。但是,初始化该引用的唯一法律要点是它的声明。您永远不能在程序中稍后更改此引用。实际上,C / C ++引用作为它所引用的原始变量的别名。



因此,C中引用的实际用途非常少/ C ++。实际上,它们通常仅用于将参数传递给函数,或者很少用于返回函数的结果。





Tl; dr:

你的问题意味着一个没有意义的解决方案。我希望我的解释清楚地告诉你。请指出问题背后的意图,以便我们提供更好的解决方案。



Note that a reference in C/C++ is always const, although many compilers will accept if you omit the const qualifier. However, the only legal point to initialize that reference is within it''s declaration. You can never change this reference later in the program. Effectively, a C/C++ reference serves as an alias for the original variable that it refers to.

As a result, there are very few real uses for references in C/C++. In fact, they are usually only used for passing parameters to functions or, rarely, returning results from functions.


Tl;dr:
Your question implies a solution that doesn''t make sense. I hope my explanations made that clear to you. Please specify the intent behind your question so we can provide a better solution.


嗨亲爱的朋友



如果你想转换char * to char使用上面的代码



hi dear friend

if you want to convert the char* to char use above code

char *a=0,*i=0,b;
a=new char[n];
i=&a[0];
b=*i;

...
i++;
b=i;
...



a是一个动态数组,我是参考,b是一个角色。



i希望它有用


a is an dynamic array,i is reference and b is a character.

i hope it will be useful


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

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