Solved

Addresses and pointers galore...

Posted on 1998-09-10
6
206 Views
Last Modified: 2010-04-02
Hi guys,
   I have a few question that I am not very sure bout.
Q1. What is the diff between a function like:
   a) char MyFunction (....)
 and
   b) char &MyFunction(...)
 What should it return in case a) and b)?
  I think that a) should return a char object while
   b) should return a pointer to a char object...
  cause a pointer to the address (as in b) is actualy the real value ( in this case, the char ).Am I right?

Second question:
Q2. I have seen Functions with parameter (datatype *&varname,...)
.. why do we need '*&'?  What should we pass in ? I think that we should pass in a object( and never a pointer ) because a pointer to the address of the object is simply the object (value) itself. Am I right again?

Thanks cause I am trying to understand pointers/addresses which I feel is quite complex to me...

David Chong
0
Comment
Question by:Haho
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1172535
Q1. Yes, you're right. Let's take an example. I am not sure if you can read Intel 80x86 assembly language. In MyFunction1(), the value of ch is returned. In MyFunction2(), the address of ch is actually returned.

1:    char MyFunction1()
2:    {
00401000   push        ebp
00401001   mov         ebp,esp
3:        static char ch = 'a';
4:        return ch;
00401003   mov         al,[___xt_z(0x0040c030)+0Ch]
5:    }
00401008   pop         ebp
00401009   ret
6:
7:    char &MyFunction2()
8:    {
0040100A   push        ebp
0040100B   mov         ebp,esp
9:        static char ch = 'a';
10:       return ch;
0040100D   mov         eax,offset ___xt_z(0x0040c034)+10h
11:   }
00401012   pop         ebp
00401013   ret
12:
13:   int main()
14:   {
00401014   push        ebp
00401015   mov         ebp,esp
00401017   sub         esp,8
15:       char ch1 = MyFunction1();
0040101A   call        MyFunction1(0x00401000)
0040101F   mov         byte ptr [ch1],al
16:       char ch2 = MyFunction2();
00401022   call        MyFunction2(0x0040100a)
00401027   mov         al,byte ptr [eax]
00401029   mov         byte ptr [ch2],al
17:
18:       return 0;
0040102C   xor         eax,eax
19:   }
0040102E   mov         esp,ebp
00401030   pop         ebp
00401031   ret


Q2. It means that varname is a reference of datatype * type. The pointer is a reference. You should pass a variable of datatype * type. For example,

datatype *pdat = ...;
Func(pdat);

The value of pdat may be changed in Func().
0
 
LVL 1

Author Comment

by:Haho
ID: 1172536
hi chensu ,
   I would like to ask something for Q2.
You said ' varname is a reference for datatype *type. I am not sure bout this statement. Does it mean that this parameter is a usual (datatype *varname ) but the value is passed by reference??
If I change varname's value, will this change be reflected outside of this function (similar to call by value) ??
Can u perhaps explain it a bit more clearly as I am not a pointer expert... :)

Any comments from the rest would be great! Thanks!

David Chong
0
 
LVL 23

Expert Comment

by:chensu
ID: 1172537
>Does it mean that this parameter is a usual (datatype *varname ) but the value is passed by reference??

Yes.

>If I change varname's value, will this change be reflected outside of this function (similar to call by value)?

Yes.

For example,

void func1(int *pn)
{
    *pn = 2;
    pn++;
}

int n = 1;
int *pn = &n;
func1(&n);
// n == 2 at this point
n = 3;
func1(pn);
// pn == &n, no change.
// n == 2 at this point


void func2(int *&pn)
{
    *pn = 2;
    pn++;
}

int n = 1;
int *pn = &n;
func2(pn);
// pn == &n + 1, changed.
// n == 2 at this point

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Haho
ID: 1172538
hi chensu,
   one more final question... :)
   So does this mean that if I want to design a function that accepts a pointer to a data type as a parameter AND I want this pointer's value to change ( call by value - similar), I would use '*&' and if I want call by reference , I would use the usual "datatype * varname" parameter..???

Thanks! Just reaffirming what u have tought me ..

David
0
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1172539
Yes. A pointer is nothing more than a data type. If you use something like void func1(int *pn), the content pointed to by pn can be changed. If you use something like void func2(int *&pn), the content pointed to by pn can be changed and the pointer itself can also be changed.
0
 
LVL 1

Author Comment

by:Haho
ID: 1172540
thanks!!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question