fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX 12
  5.  
  6. typedef struct aluno {
  7. char nome[40];
  8. int idade;
  9. } Aluno;
  10.  
  11. typedef struct lista {
  12. size_t qnt;
  13. Aluno dados[MAX];
  14. } Lista;
  15.  
  16. Lista* cria() {
  17. Lista *l = malloc(sizeof(Lista));
  18. if (l != NULL)
  19. l->qnt = 0;
  20. return l;
  21. }
  22.  
  23. int insere(Lista *l, Aluno al) { //insere no inicio
  24. if (l == NULL || l->qnt == MAX) return 0;
  25. for (int i = l->qnt; i > 0; i--) l->dados[i] = l->dados[i - 1];
  26. memcpy(&l->dados[0], &al, sizeof(Aluno));
  27. l->qnt++;
  28. return 1;
  29. }
  30.  
  31. void liberar(Lista *l) {
  32. free(l);
  33. }
  34.  
  35. void exibe(Lista *l) {
  36. if (l->qnt == 0) printf("\nLista esta vazia.\n\n");
  37. for (int i = 0; i < l->qnt; i++) {
  38. printf("Aluno: %s", l->dados[i].nome);
  39. printf(", de Idade: %d\n", l->dados[i].idade);
  40. }
  41. }
  42.  
  43. int main() {
  44. Lista *l = cria();
  45. Aluno al;
  46. int opc;
  47. do {
  48. printf("\tEscolha uma Opcao\n");
  49. printf("O: sair\n");
  50. printf("1: Cadastrar Aluno\n");
  51. printf("2: Exibir Alunos\n");
  52. printf("Opcao: ");
  53. scanf("%d", &opc);
  54. switch(opc) {
  55. case 0:
  56. break;
  57. case 1: //inconsistĂȘncia
  58. printf("\nDigite o nome: ");
  59. scanf("%s", al.nome);
  60. printf("Digite a idade: ");
  61. scanf("%d", &al.idade);
  62. insere(l, al);
  63. system("cls");
  64. break;
  65. case 2:
  66. exibe(l);
  67. break;
  68. default:
  69. printf("Opcao Invalida.\n");
  70. break;
  71. }
  72. } while (opc != 0);
  73. liberar(l);
  74. }
  75.  
  76. //https://pt.stackoverflow.com/q/171059/101
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
1
joao
10
1
jose
20
1
maria
30
2
0
compilation info
prog.c: In function ‘cria’:
prog.c:18:5: error: this ‘if’ clause does not guard... [-Werror=misleading-indentation]
     if (l != NULL)
     ^~
prog.c:20:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
         return l;
         ^~~~~~
cc1: all warnings being treated as errors
stdout
Standard output is empty