Advertisement
Guest User

Strutture - Punti X e Y con malloc()

a guest
Apr 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct {
  5.     char nome;
  6.     struct {
  7.         int X;
  8.         int Y;
  9.     }coord;
  10. }Punto;
  11.  
  12. void setPunto(Punto *P);
  13. Punto* fzCreaVett(int nP);
  14.  
  15. int main()
  16. {
  17.     int numPunti;
  18.     Punto* p;
  19.    
  20.     printf("Numero Punti: ");
  21.     scanf("%d", &numPunti);
  22.     p = fzCreaVett(numPunti);
  23.     for (int i = 0; i < numPunti; i++) // Ciclo per la funzione setPunti, dando come parametro il puntatore con un indice (quindi un vettore)
  24.     {
  25.         printf("\n%d^ punto:\n", i+1);
  26.         setPunto(&p[i]); // Usiamo gli indici per salvare i nomi e le informazione della funzione fzCreaVett
  27.     }
  28.     printf("\n\n");
  29.     for (int i = 0; i < numPunti; i++) // Ciclo per stampare le coordinate assegnate
  30.         printf("%c (%d,%d)\n", p[i].nome, p[i].coord.X, p[i].coord.Y);
  31.  
  32.     return 0;
  33. }
  34.  
  35. void setPunto(Punto *P) // Interazione I/O  
  36. {
  37.     printf("Nome: "); while (getchar() != '\n'); scanf("%c", &P->nome);
  38.     printf("X punto %c: ", P->nome); while (getchar() != '\n'); scanf("%d", &P->coord.X);
  39.     printf("Y punto %c: ", P->nome); while (getchar() != '\n'); scanf("%d", &P->coord.Y);
  40. }
  41.  
  42. Punto* fzCreaVett(int nP) // Creo dinamicamente un vettore di nP elementi
  43. {
  44.     Punto *p = (Punto*)malloc(nP * sizeof(Punto*));
  45.     return p;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement