This is a discussion on problem in pass by reference within the C++ Programming forums, part of the General Programming Boards category; solve...
Last edited by personal12; 09-25-2011 at 09:22 AM. Reason: not used
should be:Code:void main()
Now, your change function takes a pointer to int as an argument. Therefore, in the main function, you cannot pass number, but rather should pass &number to the change function. Note that we more typically refer to "pass by reference" as using a reference parameter, not a pointer parameter, even though passing a pointer has call by reference semantics with respect to what the pointer points to.Code:int main()
Incidentally, what is this program supposed to do?
Actually i have a assignment that asking me to get the array from a function then cout at another function.The user have to choose switch in order to get to the function
if i add & to the number.
it will give me address like-832455433;
When prefixed to an existing variable, & is the address of operator, eg:
When used with a function parameter or variable declaration, & indicates a reference:Code:int x, *p = &x;
Also note that this:Code:int x, &p = x; void somefunc(int &n);
is equivalent to this:Code:void change(int number)
which is why using &number works -- it creates a pointer, not a reference. As laserlight says, these are very similar things, but do confuse them as the syntax is non-compatible.Code:void change(int *number)
If you are trying to pass an array to another function, then (one of) the correct syntax(es) would be:
Very import exception here: do not use & on the array.Code:int myarray[arbitrary_size]; myfunc(myarray);
Now, you treat myarray as an array and print its contents.Code:void myfunc(int * myarray)
Merge posts Laser?
EDIT: Maybe you should consider this one as well. I think we have a homework spammer.
never mind .But please can someone tell me how come if i press 2 it will give me -89245435345 and how i can fix this.
you are not answering the question
> case 2:change(&a);break;
Passes a pointer to ONE integer.
> void change(int number)
Expects a pointer to TWO integers.
So yes, there is garbage to be found.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
It gives you that because a was not initialised, so when you pass a pointer to a, dereferencing the pointer gives you garbage. Furthermore, number is incorrect because a is a lone variable.Originally Posted by personal12
I cannot tell you how to fix this because I do not know what you are trying to do. Sure, you have stated what your assignment is about, but this does not tally with what I expect you to have written. Your change function does not change anything, so it has a lousy name. It looks like it is intended to print the first two elements of an array, yet you are trying to use it with a lone int variable.
You are not answering my questions either.Originally Posted by personal12
what do you mean by garbage to be found