473,503 Members | 12,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems with Pass by Reference

I have a function as follows -

long Read (char* databuf)
{
/* start missing */

databuf = new char [datasize];
file.read (databuf, datasize);
return datasize;
}

I call it as follows -

char* databuf = NULL;
long datasize = Read (databuf);

The problem is that databuf is still NULL after that call to Read(). I
understand why, but how should I do this?

Thanks,

Barry.

Sep 18 '06 #1
2 1644
The problem is that databuf is still NULL after that call to Read(). I
understand why, but how should I do this?
You want a reference to a pointer:

long Read(char *&p)
{
p = new char[10];
return 10;
}

int main()
{
char *p;

Read(p);

delete [] p;
}

--

Frederick Gotham
Sep 18 '06 #2
Frederick Gotham wrote:
>The problem is that databuf is still NULL after that call to Read(). I
understand why, but how should I do this?

You want a reference to a pointer:

long Read(char *&p)
{
p = new char[10];
return 10;
}

int main()
{
char *p;

Read(p);

delete [] p;
}
Yes you need a *reference to pointer* or *pointer to pointer* the reason
is that you actually passing the pointer by value - what does that mean?

When you call Read(..) a temp copy of you databuf pointer is created
pointing to NULL, this is then passed to Read(..) - so every change you
make to the databuf inside Read(..) is only performed on the temp copy
and when Read(..) returns the original databuf will remain unchanged.

Did that make sense to you?

--
// morten
Sep 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
12596
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
5
6061
by: Justice | last post by:
Currently I'm doing some experimenting with the XMLHTTP object in Javascript. Now, the XMLHttp object is asynchronous (at least in this case), and the following code causes a significant memory...
3
3657
by: Christian Christmann | last post by:
Hi, I've some problems with iterators on hashtbales. My hashtable.h class HashTable { map<int, void*> nhash; map<int, void*> getNhash();
1
965
by: Timothy V | last post by:
Hi, I'm having trouble figuring out something. How do i pass a class instance to a function without 'creating a copy'. In C/C++ i would use &. For example (C/C++): class CLASS; void...
4
1712
by: JC - home | last post by:
Hello.. I've been having some problems for a little while with this which I was sure I would beat...hmmm. Anyway, I have a form with two rich textboxes. One at the top which is to display a...
4
2274
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. ...
10
1860
by: Cliff | last post by:
Greetings, I have been trying to teach myself C++ over the past few weeks and have finally came across a problem I could not fix. I made a simple program that prints out a square or rectangle...
11
1728
by: Brad Pears | last post by:
I am using a function called "CreateSQLParam" which adds SQL parameters to a collection. The function is shown below... I add a parameter to a collection using the following line code... ...
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...
14
3767
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
0
7212
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
7098
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
7364
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...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
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
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
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
1524
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 ...
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.