473,503 Members | 4,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pass by reference

6 New Member
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
3 2957
AdrianH
1,251 Recognized Expert Top Contributor
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
9,208 Recognized Expert Moderator Expert
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
1,251 Recognized Expert Top Contributor
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

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

Similar topics

110
9812
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
4
3392
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
5
7802
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into...
4
2273
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
14
20368
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
10
13607
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
6
2698
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
11
3333
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
15
2000
by: ramif | last post by:
Does call by reference principle apply to pointers?? Is there a way to pass pointers (by reference) to functions? Here is my code: #include <stdio.h> #include <stdlib.h>
12
11005
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
7194
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.