Author Topic: C programming. How to extract two integers from a function. <SOLVED>  (Read 1161 times)

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
I'm working on a game. It has to generate two (actually more then two, hence the function) bridges at any given time randomly and they can not be at the same location.

How do I call br1 and br2 from my function into my main program? I want to make br1 = br1top and br2 = br2top. The function can only run one time. I don't really want to split it into two functions because I'm sure there is a way to do this.

This is what I have but its not working. It compiles, but I'm not sure of the syntax to get this to work. The integers are not coming back correctly for me.



Code: [Select]
       

#include <stdio.h>

int bridgefunc (int br1, int br2);

int main ()

{

     int br1top, br2top;

    //Set bridge startpoints. 2 per side.
    bridgefunc(br1top,br2top);
    printf ("br1 = %i\n",br1top);
    printf ("br2 = %i\n",br2top);

    
    system ("pause");    
    
    return 0;
}

int bridgefunc (int br1, int br2)

{
        int i;
        
        //find bridge location points via random numbers
        for (i=0;i<90000000;i++)
        {}  //delay for loop to make numbers from time more random.
        br1=rand()%6;
        printf ("function br1 = %i \n",br1);    //debug
        br2=br1;    
        printf ("function br2 = %i \n",br2);        //debug
        
        //ensures br1 does not equal br2.
        while (br2==br1)
        {
            for (i=0;i<90000000;i++)
            {}  //delay for loop to make numbers from time more random.
            br2=rand()%6;
            printf ("function br2 = %i \n",br2);     //debug
        }
        
        return (br1, br2);
}
        

« Last Edit: April 23, 2011, 02:35:09 PM by docnascar »
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Offline AS

  • Hero Member
  • *****
  • Posts: 4111
  • Have a nice ... night!
Re: C programming. How to extract two integers from a function.
« Reply #1 on: April 23, 2011, 02:17:13 PM »
Hello,

in C it's not possible to return more then one value from a function, but there are several way to work around this problem:

1) passing a pointer to an array from to calling function to the called one, which in turn will fill one or more elements of the array

2) passing a pointer to a struct, ... same as above ...

3) passing argument as pointers, in this case the results are returned in the "arguments": the following example will print out: Res1 =100, Res2 = 100

Code: [Select]
main()
{
    int result1 = 10;
    int result2 = 20;
    calculate(&result1, &result2);
    printf(" Res1 = %d,  Res2 = %d\n", result1, result2);
}

void calculate(int *r1, int *r2)

     *r1 = 100;
     *r2 = 200;
}

Cheers
AS

Online muungwana

  • Hero Member
  • *****
  • Posts: 6254
Re: C programming. How to extract two integers from a function.
« Reply #2 on: April 23, 2011, 02:20:44 PM »

you can use pointers as arguments to functions. This is one of the ways of getting multiple results from functions. It creates something called "side effects" which is a problem in parrallel computing but it shouldnt affect your single threaded code.

The first below code is my modification to your code where i use pointers as arguments to pass variables in and out of the function and the second quite is my output from the program with my changes.
Quote
     

#include <stdio.h>

void bridgefunc (int *br1, int *br2);

int main ()

{

     int br1top, br2top;

    //Set bridge startpoints. 2 per side.
    bridgefunc(&br1top,&br2top);
    printf ("br1 = %i\n",br1top);
    printf ("br2 = %i\n",br2top);

   
    system ("pause");   
   
    return 0;
}

void bridgefunc (int *br1, int *br2)

{
        int i;
       
        //find bridge location points via random numbers
        for (i=0;i<90000000;i++)
        {}  //delay for loop to make numbers from time more random.
        *br1=rand()%6;
        printf ("function br1 = %i \n",*br1);    //debug
        *br2=*br1;   
        printf ("function br2 = %i \n",*br2);        //debug
       
        //ensures br1 does not equal br2.
        while (*br2==*br1)
        {
            for (i=0;i<90000000;i++)
            {}  //delay for loop to make numbers from time more random.
            *br2=rand()%6;
            printf ("function br2 = %i \n",*br2);     //debug
        }
       
}     

Quote
[mtz@mtz ~]$ gcc test.c
[mtz@mtz ~]$ ./a.out
function br1 = 1
function br2 = 1
function br2 = 4
br1 = 1
br2 = 4
sh: pause: command not found
[mtz@mtz ~]$

.. 3 things are certain in life : death, taxes and software bloat ..
.. tell me something i don't know, something i can use as i struggle to reason with the world around me ..

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: C programming. How to extract two integers from a function.
« Reply #3 on: April 23, 2011, 02:34:52 PM »
Thanks guys   ;D

The pointer method works great for me...
Code: [Select]
       bridgefunc(&br1top,&br2top);
        printf ("br1 = %i\n",br1top);
        printf ("br2 = %i\n",br2top);
    
    system ("pause");    
    
    return 0;
}

void bridgefunc (int *x, int *y)

{
        int i,br1,br2;
        
        
        //random number seed.
        srand(time(NULL));

        //find bridge location points via random numbers
        for (i=0;i<90000000;i++)
        {}  //delay for loop to make numbers from time more random.
        br1=rand()%6;
        printf ("function br1 = %i \n",br1);    //debug
        br2=br1;    
        printf ("function br2 = %i \n",br2);        //debug
        
        //ensures br1 does not equal br2.
        while (br2==br1)
        {
            for (i=0;i<90000000;i++)
            {}  //delay for loop to make numbers from time more random.
            br2=rand()%6;
            printf ("function br2 = %i \n",br2);     //debug
        }
        *x=br1;
        *y=br2;
}
« Last Edit: April 23, 2011, 02:57:07 PM by docnascar »
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Online muungwana

  • Hero Member
  • *****
  • Posts: 6254
Re: C programming. How to extract two integers from a function. <SOLVED>
« Reply #4 on: April 23, 2011, 02:57:24 PM »
In C, a function can not return more than one data structure but you can work around this by creating a compound data structure and then have the function return that one compound data structure made up of multiple simple data structures. You can use this if you dont want to deal with pointers to get multiple data structures from functions.

a compound data structure is created with a key work "struct" as shown below.

A compound data structure "XYZ" is created with two "int" data structures.
Once you have this data structure, you can move around it as if it is a simple data structure and you access its individual components using the "."

Example below, as your home work, can you modify your code to make it return those two results as one entity?  :D
Code: [Select]
     
#include <stdio.h>

struct XYZ
{
  int x, y ;
} ;

struct XYZ multiply(int x, int y)
{
    struct XYZ  z ;
    z.x = x * 2 ;
    z.y = y * 2 ;
    return z ;
}
int main(void)
{
   struct XYZ z ;
  
   z = multiply( 3, 7 ) ;
  
   printf("%d\n",z.x) ;
   printf("%d\n",z.y) ;  
   return 0 ;
}

Output of above code is

Code: [Select]
[mtz@mtz ~]$ ./a.out
6
14
[mtz@mtz ~]$



.. 3 things are certain in life : death, taxes and software bloat ..
.. tell me something i don't know, something i can use as i struggle to reason with the world around me ..