home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / CataniaB / teach-act / esempi / Strutture_Union / struct1.c < prev    next >
C/C++ Source or Header  |  1997-04-06  |  1KB  |  69 lines

  1. #include <stdio.h>
  2.  
  3. /* Record in C definiti con structure */
  4.  
  5. struct { float x; float y; }  R;      /* dichiarazione di variabile */
  6.  
  7. struct   punto   {float x; float y;}; /* dichiarazione del tipo "punto" */
  8.  
  9. struct   punto    Q1,Q2;              /* variabili di tipo  punto*/
  10.  
  11. struct   punto    P={1.5,2.8};        /* iniz.  con lista di costanti */
  12.     
  13. struct   punto1 { float x,y,z; } S,T; 
  14.                            /* dichiarazione del tipo e delle variabili */
  15.  
  16.  
  17. /* E' possibile usare la dichiarazione "typedef" per ridenominare 
  18.    nomi di tipo:  typedef TIPO NOME; */
  19.  
  20. typedef long myint;
  21.  
  22. typdef struct {float x,y;} point;  
  23.   /* equivale a "struct point {float x,y;};"  */
  24.  
  25. point MP;  /* non serve struct  */
  26.  
  27.  
  28. /*  esempio limite il nome della struttura, dei campi, e 
  29.  *  delle varibili possono essere uguali */ 
  30.  
  31. struct  s {int s,z;};  
  32. struct  s s; 
  33.  
  34.  
  35. typedef struct {int el;} el;
  36. el w; 
  37.  
  38. /* NON va bene: el el; */ 
  39.  
  40. main()
  41. {
  42.  
  43. /* accesso ai campi  selettore "." 
  44.  * I record si possono assegnare ma non confrontare direttamente!
  45.  */
  46.  
  47.  printf("\n %f %f",P.x,P.y);
  48.  
  49.  Q1=P;       /* Q1 e P hanno lo stesso tipo! */
  50.  Q2.x=P.x;
  51.  Q2.y=P.y;
  52.  
  53.  P.x=P.y;
  54.  
  55. /* R=P;       Errore di tipo! (sorg. e dest. devono avere lo stesso tipo) */
  56.  
  57.  R.x=P.x;    /* campo x campo */ 
  58.  R.y=P.y;
  59.  
  60. /* Oss:  Per confrontare  R e P: componente per componente! */
  61.  
  62.  if (P.x==R.x && R.y==P.y);  
  63.  
  64. /* NON si puo' usare direttamente: (P==R) */
  65.  
  66. }
  67.  
  68.  
  69.