9

Passing arguments by address

  1. #include <stdio.h>
  2.  
  3. void swap( int *x, int *y ) {
  4.     int t = *x;
  5.     *x = *y;
  6.     *y = t;
  7. }
  8.  
  9. main() {
  10.     int a = 11, b = 99;
  11.  
  12.     printf( "a=%d, b=%d\n", a, b );
  13.     swap( &a, &b );
  14.     printf( "a=%d, b=%d\n", a, b );
  15. }
$ gcc -o swap swap.c
$ ./swap
a=11, b=99
a=99, b=11
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void circ( const double *r, double *area, double *len ) {
  5.     const double PI = 3.14159265358979323846;   /* see M_PI in math.h */
  6.     *area = PI**r**r;
  7.     *len = 2*PI**r;
  8. }
  9.  
  10. int main( int argc, char *argv[] ) {
  11.     double r, a, l;
  12.  
  13.     switch ( argc ) {
  14.         case 2:
  15.             r = atof( argv[1] );
  16.             circ( &r, &a, &l );
  17.             printf("%f %f\n", a, l);
  18.             break;
  19.         default:
  20.             printf( "%s radius\n", argv[0] );
  21.             exit(1);
  22.     }
  23.     exit(0);
  24. }
$ gcc -o circ circ.c
$ ./circ
./circ radius
$ ./circ 1
3.141593 6.283185

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].