All pastes #498518 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-05-20 14:11 UTC
rendered paste body
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

// swap these
//typedef vector<int> thetype;
typedef long thetype;


struct mystru
        {
            int a;
            thetype b;
            double c;
        };

void struacceptor( struct mystru stru )
{
    int diffa = ((int)&stru.b-(int)&stru.a);
    char ca = ( diffa < 0 ? '-' : '+' );
    int diffb = ((int)&stru.c-(int)&stru.b);
    char cb = ( diffb < 0 ? '-' : '+' );

    cout << "struA loc = " << &stru.a << " " << endl
         << "struB loc = " << &stru.b << " (A" << ca << "0x" << hex << ( diffa < 0 ? abs( diffa ) : diffa ) << ") " << endl
         << "struC loc = " << &stru.c << " (B" << cb << "0x" << hex << ( diffb < 0 ? abs( diffb ) : diffb ) << ") " << endl;    
}
        
int myFunction( int a, thetype b, double c ) 
{
    int diffa = ((int)&b-(int)&a);
    char ca = ( diffa < 0 ? '-' : '+' );
    int diffb = ((int)&c-(int)&b);
    char cb = ( diffb < 0 ? '-' : '+' );
    
    cout << "A loc = " << &a << " " << endl
         << "B loc = " << &b << " (A" << ca << "0x" << hex << ( diffa < 0 ? abs( diffa ) : diffa ) << ") " << endl
         << "C loc = " << &c << " (B" << cb << "0x" << hex << ( diffb < 0 ? abs( diffb ) : diffb ) << ") " << endl
         << endl;
    cout << "A = " << a 
         //<< " B = " << b
         << " C = " << c << endl;
    return 0;
}

int main( int argc, char *argv[] )
{
    vector<int> b;
    b.push_back( 1 );
    b.push_back( 2 );
    b.push_back( 4566 );
    
    // Change this from 5 to b if you're using vector type
    myFunction( 1, 5, 5.3 );
    
    mystru stru;
    
        
    int diffa = ((int)&stru.b-(int)&stru.a);
    char ca = ( diffa < 0 ? '-' : '+' );
    int diffb = ((int)&stru.c-(int)&stru.b);
    char cb = ( diffb < 0 ? '-' : '+' );

    cout << "struA loc = " << &stru.a << " " << endl
         << "struB loc = " << &stru.b << " (A" << ca << "0x" << hex << ( diffa < 0 ? abs( diffa ) : diffa ) << ") " << endl
         << "struC loc = " << &stru.c << " (B" << cb << "0x" << hex << ( diffb < 0 ? abs( diffb ) : diffb ) << ") " << endl;
    
    stru.a = 3;
    //stru.b = 0x12345;
    stru.c = 5.3;
    //cout << "b loc = " << &(stru.b) <<  endl;
    
    struacceptor( stru );
    
    typedef int (*fptr)( struct mystru );
    
    fptr f = (fptr)( myFunction );
    
    cout << "2nd call:" << endl;
    f( stru );
    cout << "Call 2" << endl;
    
    return 0;
}