473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

pass by reference

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 2951
AdrianH
1,251 Expert 1GB
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 Expert Mod 8TB
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 Expert 1GB
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
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
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
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
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
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
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
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
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
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
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.