Pointers and structures
This example shows how to work with pointers to structures.


Source Code: (main.c)
#include <stdio.h>

typedef struct {
  double x,y;
complex;

void print(z)
     complex z;
{
  printf("%1.2lf + %1.2lf * I\n"z.xz.y);
}

void square(z)
     complex *z/* take a pointer to a complex number */
{
  double x,y;
  x = z->x * z->x - z->y * z->y;  /* for pointers z->x replaces z.x */
  y = 2 * z->x * z->y;
  z->x = x;
  z->y = y;
}

int main(){
  complex z={2.0,3.0};
  printf("z = ");
  print(z);
  
  square(&z);
  printf("After \"square(z);\" z = ");
  print(z);

  return 0;
}

Output:
z = 2.00 + 3.00 * I
After "square(z);" z = -5.00 + 12.00 * I

To compile:
gcc main.c -o main


HOME >>>>> PROGRAMMING >>>>> POINTERS