By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 791 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

pass by reference

P: 6
how to pass char* value by creating new?

here function definition is

ConvertNumberToString(double source, char *targetString)
{

}

wil u pleas tell me how to pass (char* targetstring) value in main() bymeans of creating memory by 'new' method?
Also explain Different types pass reference methods in c++?
Jun 9 '07 #1
Share this Question
Share on Google+
3 Replies


AdrianH
Expert 100+
P: 1,251
how to pass char* value by creating new?

here function definition is

ConvertNumberToString(double source, char *targetString)
{

}

wil u pleas tell me how to pass (char* targetstring) value in main() bymeans of creating memory by 'new' method?
Also explain Different types pass reference methods in c++?
well you know the type you want to return, char *, so put that in front of the name of the function.

To allocate a char array, you use new char [dim_size] in C++ and malloc(sizeof(char)*dim_size) in C. Note that the sizeof(char) is a little redundent, but is used exemplify how you would allocate for any array. Also note that in C, you do not have to cast to a char* when assigning it to a char*. malloc() returns a void * and a void * in C can be assigned to any pointer type and vice-versa.

Enjoy.


Adrian
Jun 9 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Quote:
Originally Posted by Prabu Ramasamy
how to pass char* value by creating new?

here function definition is

ConvertNumberToString(double source, char *targetString)
{

}

wil u pleas tell me how to pass (char* targetstring) value in main() bymeans of creating memory by 'new' method?
Also explain Different types pass reference methods in c++?

well you know the type you want to return, char *, so put that in front of the name of the function.
You can also have the calling function pass in the address of the targetString pointer.

Expand|Select|Wrap|Line Numbers
  1. void ConvertNumberToString(double source, char **targetString)
  2. {
  3.  
  4. }
  5.  
Then, inside ConvertNumberToString you:

Expand|Select|Wrap|Line Numbers
  1. *targetString = new char[...some size...];
  2.  
and that changes the address inside the pointer used by the calling function.
Expand|Select|Wrap|Line Numbers
  1. char* ptr = 0;
  2. ConvertNumberToString(2.5, &ptr);
  3. cout << ptr;  //you see your string.
  4.  
Jun 9 '07 #3

AdrianH
Expert 100+
P: 1,251
Also, instead of using a pointer to a pointer, you can use a reference to a pointer. i.e char *& targetString instead of char ** targetString.

Of course, using cstrings in C++ is not necessary as wfc usually points out. You are better off using the string class.

Adrian
Jun 9 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.