[英]Trouble with FIFO
I'm trying to create FIFO with struct but it's not working, I'm trying to find out what's wrong with my code since yesterday night. 我正在尝试使用struct创建FIFO,但无法正常工作,我试图找出自昨天晚上以来我的代码出了什么问题。 It's only prototype and so far everything my code does is store information. 它只是原型,到目前为止,我的代码所做的所有事情都是存储信息。 Apparently it is not doing even it. 显然,它甚至没有这样做。
#define TAM 10
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
typedef struct
{
int final;
int inicio;
int tamanho;
int contador;
char *vetorFila[TAM];
} pretendentes;
void entrarComDadosLista();
void armazenarDadosLista();
int main(void)
{
setlocale(LC_ALL, "");
int i;
for (i = 0; i < TAM; i++)
{
pretendentes -> vetorFila[i] = NULL;
}
entrarComDadosLista();
return 0;
}
void entrarComDadosLista()
{
static char nomes[10], *ptr;
printf("Digite o nome ou aperte ENTER para sair.\n\n");
do
{
printf("Digite o nome do pretendente: ");
scanf("%s", nomes);
ptr = (char*) malloc(strlen(nomes));
strcpy(ptr, nomes);
armazenarDadosLista(ptr);
} while(*nomes);
}
void armazenarDadosLista(pretendentes *Pretendente, char *nomes)
{
if(Pretendente -> inicio == TAM)
{
printf("A fila está cheia.");
return 0;
}
Pretendente -> vetorFila[Pretendente->inicio] = nomes;
Pretendente -> inicio++;
}
Can someone give me hand to find my error? 有人可以帮我找到我的错误吗? The code does compile but it stops working after few entries. 该代码可以编译,但是在输入几次后便停止工作。 I just wanna check if the names are being stored in the fifo. 我只想检查名称是否存储在fifo中。
Here is what I found: 这是我发现的:
// (1) declare "armazenarDadosLista" as a function accepting
// an unknown number of arguments that each have an unknown type
void armazenarDadosLista();
...
// (2) in "entrarComDadosLista", call "armazenarDadosLista" with
// 1 argument: ptr
armazenarDadosLista(ptr);
...
// (3) declare "armazenarDadosLista" as a function accepting
// 2 arguments: pretendentes* , char*
void armazenarDadosLista(pretendentes *Pretendente, char *nomes) {
}
If you change (1)
to match (3)
, the compiler will complain because you call armazenarDadosLista
with an incorrect number of arguments. 如果将(1)
更改为与(3)
匹配,则编译器将抱怨,因为您使用错误的参数数量调用armazenarDadosLista
。 Instead of using void function_name();
而不是使用void function_name();
for all functions, declare them with the correct number and types of arguments, so the compiler can help you when you do something like armazenarDadosLista(ptr)
: 对于所有函数,请使用正确数量和参数类型声明它们,以便在执行诸如armazenarDadosLista(ptr)
类的armazenarDadosLista(ptr)
时,编译器可以为您提供帮助:
void entrarComDadosLista(void);
void armazenarDadosLista(pretendentes *Pretendente, char *nomes);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.