Problems passing values from function to main variables

This is a discussion on Problems passing values from function to main variables within the C Programming forums, part of the General Programming Boards category; Originally Posted by matsp And for most cases, the CORRECT solution. That's why it also says "and generally best"...are you ...

  1. #16
    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

  2. #17
    Registered User
    Join Date
    Jun 2009
    Posts
    12
    Well, I arrived to this:

    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; 
    }
    
    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=SV(cat); 
      
      
       for(i=0;i<12;i++)
      {
    	  { for(j=0;j<3;j++)
                     printf("mouse %8.3f\t", mouse[i][j]);
              }
                           printf("\n");
         }
       
       
       return 0;
    }
    This only prints the first element, [0][0].
    can you please tell what am I doing wrong?
    thanks in advance

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because you are still only returning a single float value - just going via a double-pointer.

    I would pass the mouse array into the SV function (along with the cat array).

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

  4. #19
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    I think return dog will do

Page 2 of 2 FirstFirst 12
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, 06:30 PM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21