solve
Printable View
solve
This:
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.
[code]
case 2:change(&number);
[code]
it will give me address like-832455433;
Maybe you are confused by something about &.
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)
Sorry, but your code looks incoherent considering what you stated as your assignment.
What do you mean by "the user have to choose switch in order to get to the function"?
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.
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[1] is incorrect because a is a lone variable.Quote:
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.Quote:
Originally Posted by personal12
what do you mean by garbage to be found