【发布时间】:2021-05-30 21:09:40
【问题描述】:
所以,这段代码(或至少这部分代码)的目标是我将在“地籍”中输入人员的信息,然后用“mostra”显示这些人的所有信息。我尝试使用指针,因为我无法对主要功能(老师的指示)做任何事情,但由于某种原因它不起作用,指针对我来说仍然是一个谜。我尝试查看其他具有相同“通过参数 1...”问题的帖子,但它们并没有真正帮助我,因为那里的问题似乎略有不同。 我最终在 cadastra、mostra 和 mostra1 中收到“警告:从不兼容的指针类型 [-Wincompatible-pointer-types] 传递 '...' 的参数 1”菜单功能里面。 (我还没有开始mostra1的代码,因为我想在去那里之前解决这个问题,所以你可以忽略它。)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct prof {
char nome[50];
char matricula[50];
char email[50];
int ativo;
int dia, mes, ano, hora, min;
};
void cadastro(struct prof *P[100]) {
char nome1[50];
char matricula1[50];
char email1[50];
int ativo, exit;
int dia1, mes1, ano1, hora1, min1, last = 0;
do {
printf("\n----------------");
printf("\n1- Digite seu Nome: ");
scanf("%s", nome1);
getchar();
printf("\n2- Digite seu email (exemplo@ufu.br): ");
scanf("%s", email1);
getchar();
printf("\n3- Digite sua matricula: ");
scanf("%s", matricula1);
getchar();
printf("\n4- Data (dia mes ano): ");
scanf("%d/%d/%d", &dia1, &mes1, &ano1);
getchar();
printf("\n5- Horario (hora minutos): ");
scanf("%d:%d", &hora1, &min1);
getchar();
printf("----------------\n");
for (int i = last; i < last + 1; i++) {
if (P[i] -> ativo == 0) {
P[i] -> dia = dia1;
P[i] -> mes = mes1;
P[i] -> ano = ano1;
P[i] -> hora = hora1;
P[i] -> min = min1;
P[i] -> ativo = 1;
strcpy(P[i] -> nome, nome1);
strcpy(P[i] -> matricula, matricula1);
strcpy(P[i] -> email, email1);
last++;
break;
}
}
printf("\n\n1- Continuar\n0 - Sair\n");
printf("\nDigite opcao: ");
scanf("%d", &exit);
} while (exit != 0);
}
void mostra(struct prof *P[100]) {
system("cls");
int exit;
printf("\nLista de reservas\n");
do {
for (int i = 0; i<100; i++) {
if (P[i] -> ativo == 1) {
printf("----------------\n");
printf("1- Nome: %s\n", P[i] -> nome);
printf("2- E-mail: %s\n", P[i] -> email);
printf("3- Matricula: %s\n", P[i] -> matricula);
printf("4- Data: %d/%d/%d\n", P[i] -> dia, P[i] -> mes, P[i] -> ano);
printf("5- Hora: %d:%d\n", P[i] -> hora, P[i] -> min);
printf("----------------\n");
}
printf("\n1- Continuar\n0 - Sair\n");
printf("\nDigite opcao: ");
scanf("%d", &exit);
getchar();
}
} while (exit != 0);
}
void mostra1(struct prof P[100]) {
}
void menu() {
int opcao;
struct prof P[100];
while (1) {
printf("\nBem vindo ao Sistema de Sistema de Informacao para controle das reservas dos laboratorios e salas da UFU\n");
printf("\n1- Cadastrar ");
printf("\n2- Mostrar Todos");
printf("\n3- Mostrar um");
printf("\n9- Sair ");
printf("\nDigite opcao: ");
scanf("%d", &opcao);
if (opcao == 1) cadastro(&P);
if (opcao == 2) mostra(&P);
if (opcao == 3) mostra1(&P);
if (opcao == 9) return;
}
}
int main() {
menu();
}
【问题讨论】:
-
在函数
cadastro和mostra的声明中,尝试删除参数中的星号,因此struct prof P[100]而不是struct prof *P[100],如函数mostra1。现在,这两个函数需要一个指向结构的指针数组,而不是结构数组 -
谢谢,现在可以使用了。虽然我仍然收到“从不兼容的指针类型 [-Wincompatible-pointer-types] 传递'...'的参数 1”警告...
-
你在哪一行得到错误?
-
哦等等,在
menu()函数里面,好的,所以,在4个ifs中,在调用不同的函数时删除&。数组变量根据定义已经是一个指针,所以你不需要传递它的地址来处理数组 -
欢迎来到 Stack Overflow。请阅读About 和How to Ask 页面,还请阅读有关如何创建 MCVE(Minimal, Complete, Verifiable Example — 或 MRE 或 SO 现在使用的任何名称)或 SSCCE(Short, Self-Contained, Correct Example)的信息 — 同样的想法来自不同的名字。