Thread: Problems passing values from function to main variables

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    @MK: I know the OP is not returning an array, because arrays are not returnable. The OP quite clearly wants to return an array, as is stated.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    12
    Thank you for the quick reply, I will try some changes.
    Would you suggest me to use a global variable to assign the resulting array from function?

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ERJuanca View Post
    Thank you for the quick reply, I will try some changes.
    Would you suggest me to use a global variable to assign the resulting array from function?
    No, you can return a pointer to an array.


    @the world: I was *wrong*.
    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

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Returning the pointer to an array is quite involved (when the array is 2D, you need a loop and stuff). I still think it's a better idea to pass the original array to be filled in, into the function. (This may be allocated dynamically if required).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    No, you can return a pointer to an array.
    You can, but (1) I know it would take me at least three tries to get the syntax for that right and (2) even so, since array names are not assignable you can't do anything with it (like assign mouse as desired).

    [Attempt 1:
    Code:
    float *(bob(float param[12][3]))[12][3]
    But I don't think that's right, obviously.]
    @op: Just pass in the array you want changed and change it inside the function.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    @op: Just pass in the array you want changed and change it inside the function.
    That would be the "quick, easy and generally best" way.
    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

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    That would be the "quick and easy" way.
    And for most cases, the CORRECT solution.

    It is far too often on this forum that people suggest dynamic allocation as a solution. It is REALLY not the right thing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    And for most cases, the CORRECT solution.
    That's why it also says "and generally best"...are you trying to misquote me?

    This reminds me of a story I heard once about three gunslingers in a darkened closet...maybe it was two gunslingers and a grizzly bear*.

    * I think the punchline was "WHAT HAPPENED TO THE NEW G'DAM CAT?"
    Last edited by MK27; 06-09-2009 at 04:51 PM.
    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

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    @MK: I know the OP is not returning an array, because arrays are not returnable. The OP quite clearly wants to return an array, as is stated.
    I do not think the OP does want that. I think "pass the resulting array" is a typo. The rest of the context pretty much implies we want a single float value. I am right*!

    Quote Originally Posted by ERJuanca View Post
    Code:
    return dog[i][j]; //will this work? or should I use some other syntaxis?
    *it is because i & j are now out of bounds.

    ps. "syntaxis" beat's brewbuck's "happenstance" for my word o' the day...
    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

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    I do not think the OP does want that. I think "pass the resulting array" is a typo. The rest of the context pretty much implies we want a single float value. I am right*!



    *it is because i & j are now out of bounds.
    Given that he is trying to assign the entire mouse array with one function call (note that function call isn't in a loop!), why do you think this?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. passing variables from main to another function
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-06-2006, 07:30 PM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM