Sign in to follow this  

Passing arguments by references

This topic is 2565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Let's pretend that there exists a compiler that accepts multiple indirections of an object, and that it's possible in C++ (By all means, just pretend. :D)

I have this function:

void function (int & a)
{
std::cout << *(&(*(&(*(&a)))));
return;
}





Do you think that this "&" symbol can negate "*" symbol? What are the differences between these two?

Now, we're back to real-life C++:

void function (int & a)
{
std::cout << [SOMETHING];
}




What should I put in [SOMETHING] that makes this function correct and is able to print out the int a?

Thanks in advance.

Share this post


Link to post
Share on other sites
Quote:

Let's pretend that there exists a compiler that accepts multiple indirections of an object, and that it's possible in C++ (By all means, just pretend. :D)

This is allowed.

Quote:

Do you think that this "&" symbol can negate "*" symbol? What are the differences between these two?

This sounds like homework. Do you know what happens when you use the unary & operator? What about the unary * operator? What are the types of the resulting expressions?

Quote:

What should I put in [SOMETHING] that makes this function correct and is able to print out the int a?

There is no special syntax required for getting the value of the reference - the reference can be considered an alias for the original value.

Share this post


Link to post
Share on other sites
No, it's not homework, although it sounds like one. This is something I'm trying to tackle all by myself.

& : It gives the address of the value of something.
* : It gives the value of the address of something.

The results give an error. Error of In-direction.

However, it wouldn't let me do "pass by reference from an argument, and get the value of the passed reference's value the reference address is referencing."

Wordy...

So, for std::cout, I could get what the passed argument's value is, right?

It wouldn't work for function arguments that way...


int GCF (int a, int b)
{
int gcf = 0;
while (true)
{
gcf = a % b;
if (gcf == 0)
return b;
a = b;
b = gcf;
}
}

int foo (int & a, int & b)
{
return GCF (a, b); //ERROR
}



Share this post


Link to post
Share on other sites
There's nothing wrong with the codes you have posted. The chaining of &/* pairs is valid, to output the value the reference refer to you just output the reference itself (std::cout << a), and there's no error with the call to GCF.

What problems are you having? If the codes you posted produces errors for you, you probably have a broken compiler or you're not telling us the true story.

Share this post


Link to post
Share on other sites
So what exact errors are you getting in, say, the last example you posted? Copy and past the error message to not miss any detail. If I copy and past the code as it is into an empty file and compile, I get no errors at all.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Are you compiling the code as C++? References are not available in C, and the compiler might be confused by the syntax if you try to use them.


Yeah, that's the problem...Guess it's not a faulty compiler after all. :D

Well, now that I've found th real culprit, there's no need to ask more about this (solved) problem. Anyway, thank you everyone. :)

Share this post


Link to post
Share on other sites

This topic is 2565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this