Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC VC++
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;

Is there a possibility of similar coding option in C++?
 
Thanks in advance!
Posted 11-Mar-13 0:37am
Comments
Ian A Davidson at 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 at 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.
Pranit Kothari at 11-Mar-13 7:12am
   
char& characterReference; will not work.. so rewrite your question.
max_nowak at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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'
 
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
CPallini at 11-Mar-13 9:40am
   
Warning: your deferencing a NULL pointer.
Stefan_Lang at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 11 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100