9
Passing arguments by address
- #include <stdio.h>
- void swap( int *x, int *y ) {
- int t = *x;
- *x = *y;
- *y = t;
- }
- main() {
- int a = 11, b = 99;
- printf( "a=%d, b=%d\n", a, b );
- swap( &a, &b );
- printf( "a=%d, b=%d\n", a, b );
- }
$ gcc -o swap swap.c
$ ./swap
a=11, b=99
a=99, b=11
- #include <stdio.h>
- #include <stdlib.h>
- void circ( const double *r, double *area, double *len ) {
- const double PI = 3.14159265358979323846; /* see M_PI in math.h */
- *area = PI**r**r;
- *len = 2*PI**r;
- }
- int main( int argc, char *argv[] ) {
- double r, a, l;
- switch ( argc ) {
- case 2:
- r = atof( argv[1] );
- circ( &r, &a, &l );
- printf("%f %f\n", a, l);
- break;
- default:
- printf( "%s radius\n", argv[0] );
- exit(1);
- }
- exit(0);
- }
$ gcc -o circ circ.c
$ ./circ
./circ radius
$ ./circ 1
3.141593 6.283185
Comments