Thread: Problems passing values from function to main variables

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    12

    Problems passing values from function to main variables

    Hi there! this is my first post, I'm relatively new to C, so I hope to be clear,
    so here we go:

    I've made a function that works properly, but I fail to pass the resulting array from my function to the main function, here is the code,

    Code:
    #include <stdio.h>
    
    float SV(float dog[12][3]) 
    {
    	int i,j;
    	for(i=0;i<12;i++)
      {
           { for(j=0;j<3;j++)
               printf("dog %8.3f\t", dog[i][j]);
          }
                   printf("\n"); 
      }
       
    return dog[i][j]; //will this work? or should I use some other syntaxis?
    }
    
    int main()
    {
    float cat[12][3]={
    {-4.859,0.073,0.122},
    {-3.610,-0.654,0.017},
    {-2.432,0.308,0.088},
    {-2.621,1.515,0.217},
    {-1.213,-0.231,0.004},
    {-0.016,0.584,0.061},
    {1.100,-0.061,-0.749},
    {1.157,-1.283,-0.869},
    {1.989,0.764,-1.306},
    {3.095,0.267,-2.100},
    {3.942,1.426,-2.607},
    {3.646,2.585,-2.327},
    };
       
       
       float mouse[12][3];
       int i,j;
       
       for(i=0;i<12;i++)
       for(j=0;j<3;j++)
       mouse[i][j]=0;
       
       mouse[i][j]=SV(cat); //I would like to pass the return array from function SV to mouse variable 
      
       for(i=0;i<12;i++)
      {
    	  { for(j=0;j<3;j++)
                     printf("mouse %8.3f\t", mouse[i][j]);
              }
                           printf("\n");
         }
       
       
       return 0;
    }
    I would really appreciate any help,
    thank you,

    Juan Pablo

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Arrays are not returnable. But arrays that are passed in to a function are editable; any changes made to dog inside the function SV are visible from main.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Possibly because the for loop condition means "when i == 12", which is to say the loop ends because at the last iteration, i is incremented (i++), making it 12, stopping the loop.

    So when you return dog[i][j], you are returning dog[12][3]. Try "return [i-1][j-1]".

    @tabstop: the OP is not returning an array...
    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
    The code you have written will not return an array. In fact, C does not have a facility to return an array from a function. The most suitable solution is to pass in an already exisitng array, and let the functiokn fill the array in.

    --
    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
    @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.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    @matsp: the OP is not returning an array, or trying to...
    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
    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?

  8. #8
    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

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    but I fail to pass the resulting array from my function to the main function
    I would like to pass the return array from function SV to mouse variable
    To me, both of those statements sounds like an attempt to do just what you are saying OP is not doing. Clearly it is NOT doing what OP wants to do, but that's not, in my view, changing the intention.

    --
    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.

  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?

  11. #11
    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

  12. #12
    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.

  13. #13
    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.

  14. #14
    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

  15. #15
    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.

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