473,756 Members | 7,019 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass by Pointer * or Pass by Reference &?

Hello all,

I am a C programmer learning C++. And I am confused with function Pass
by Pointer * or Pass by Reference &.

Function pass by pointer like:
void func(int * x)
And function pass by referencelike:
void func(int & x)

What's their main difference and be used in what circumstance?

Best regards,
Robert

Aug 19 '05
10 40662
Robert wrote:
Another problem is
why reference cannot be able to represent [an invalid] object? cat main.cc #include <iostream>

void f(const int& r) {
if (0 == &r) { // undefined behavior
std::cout << "&r = " << &r << std::endl;
}
else {
std::cout << "r = " << r << std::endl;
}
}

int main(int argc, char* argv[]) {

int* p = 0;
int& r = *p; // undefined behavior
f(r);

return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main

&r = 0

Beware!
A reference *can* represent an invalid object
but the behavior is undefined.
Aug 24 '05 #11

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

Similar topics

3
9438
by: sam pal | last post by:
I have the following programs and please clear my doubts. Passing the value by reference is same as pointer by reference. Is this true? What is the difference between display and display2? How can I change display1 to value by reference if there is difference? void display(int &i){// Passing value by reference
2
2783
by: Nelson | last post by:
Hi, Can pointers be passed by reference to functions? If so, how to do it? Or is there any other way to change the pointers in the caller by the called function? Thanks, Nelson
3
1340
by: dover | last post by:
There are two situations under which I am not sure which one to use, pointer or reference: Passing parameter into a function A object is aggregated in another one. Is there any general rules of thumb about this? Many thanks!
41
8339
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by reference?? Thanks, BB
5
1935
by: DamonChong | last post by:
Hi, I am still struggling to master C++ and is trying to understand how to achieve passing arguments using pointers. I got some questions that I like to post to the experts here, hope you can help to clarify my doubts. I'm using g++ version 3.3.4. I created 3 classes as below for testing some concepts. The questions are written as comments in Bclass.h file. Thank you for your time and tips! ------------runtime errors------------
1
2555
by: Scott McFadden | last post by:
What is the proper way to pass pointers by reference from managed c++ calls to native c++ calls? In managed C++, I have a pointer to an array of structures, that I pass to a native c++ method by reference: //Managed c++ QueryResult* qr = NULL; ExecuteQuery(1, "abc", qr); //invoke native c++ method passing qr by reference
3
2003
by: Scott | last post by:
Hello, I am having some difficulty with the following C++: I have a declared a pointer to Foo like so: Foo *pFoo; Now, I have an API object that I try to use like so:
29
3649
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
18
9076
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the pointer, the size is not fixed. I remember, all new statement should be followed by a delete statement, is there some memory leak here?
11
8416
by: HSeganfredo | last post by:
Folks, I want to write a init string function that mallocs an area, fills it with a char, sticks a NUL char in the last position and returns it to the user. So far I noticed that my implementation always works over a NEW memory area, and not over the ORIGINAL pointer area, so the initial string area is untouched, the pointer is not update to refer to the new initiated area...
0
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2666
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.