Thread: problem in pass by reference

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    7

    problem in pass by reference

    solve
    Last edited by personal12; 09-25-2011 at 09:22 AM. Reason: not used

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This:
    Code:
    void main()
    should be:
    Code:
    int 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.

    Incidentally, what is this program supposed to do?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    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;

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by personal12 View Post
    The user have to choose switch in order to get to the function
    if i add & to the number.
    Maybe you are confused by something about &.

    When prefixed to an existing variable, & is the address of operator, eg:

    Code:
    int x, *p = &x;
    When used with a function parameter or variable declaration, & indicates a reference:

    Code:
    int x, &p = x;
    void somefunc(int &n);
    Also note that this:
    Code:
    void change(int number[])
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you are trying to pass an array to another function, then (one of) the correct syntax(es) would be:
    Code:
    int myarray[arbitrary_size];
    myfunc(myarray);
    Very import exception here: do not use & on the array.

    Code:
    void myfunc(int * myarray)
    Now, you treat myarray as an array and print its contents.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Merge posts Laser?

    EDIT: Maybe you should consider this one as well. I think we have a homework spammer.
    Last edited by AndrewHunter; 09-25-2011 at 01:13 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    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.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by personal12 View Post
    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.
    Don't press 2.......
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    you are not answering the question

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by personal12
    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.
    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.

    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.

    Quote Originally Posted by personal12
    you are not answering the question
    You are not answering my questions either.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by laserlight View Post
    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.

    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.
    As least someone has a sense of humor....
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    what do you mean by garbage to be found

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by personal12 View Post
    what do you mean by garbage to be found
    Did you even look at the code you posted? You declare a as an int and then pass it to your function as an array. Pay attention to what Laser is saying.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass by reference
    By jrice528 in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2007, 01:02 PM
  2. Pass by reference vs pass by pointer
    By Non@pp in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2006, 01:06 PM
  3. pass by reference .... HELP!!
    By NamelessNoob in forum C++ Programming
    Replies: 19
    Last Post: 02-15-2006, 12:50 PM
  4. Pass by reference
    By mcgeady in forum C Programming
    Replies: 11
    Last Post: 02-17-2005, 03:01 AM
  5. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:03 PM