Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys,

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

for example:

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

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

Thanks in advance!
Posted
Comments
Ian A Davidson 11-Mar-13 5:44am    
Would you like to give a meaningful code example of what you are trying to do? The example given appears to be illegal C++ and contains a set of randomly named variables that don't reference each other or anything else.
Regards,
Ian.
Matthew Faithfull 11-Mar-13 5:50am    
"I want to convert char* array to character reference".
I'm fairly certain that this is not really what you want to do at all. Given that you're having no luck with code try explaining what you're trying to achieve at a slightly higher level in English and there's a good chance that someone will be able to help you.
[no name] 11-Mar-13 7:12am    
char& characterReference; will not work.. so rewrite your question.
max_nowak 11-Mar-13 8:37am    
I'm sure you don't want an array of char pointers. Strings in C are represented by array of chars (char[]), which is basically a pointer to first char in the memory block (char*). You can store the reference of this by simply doing char* c = 'c'; char &ref = c; but this would be absolutely pointless and leading to nowhere. Just explain what your goal is, so people here can show you how to do it the right way. obviously your approach is not the way to go.

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

char NonConstString[] = "test";
char& Ref = *NonConstString;
 
Share this answer
 
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:
C++
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'


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.
 
Share this answer
 
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 is an dynamic array,i is reference and b is a character.

i hope it will be useful
 
Share this answer
 
v2
Comments
CPallini 11-Mar-13 9:40am    
Warning: your deferencing a NULL pointer.
Stefan_Lang 11-Mar-13 9:42am    
That is not an answer for two reasons:
1. b is not a reference
2. a is not a char _array_

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900