在C中将参数struct传递给函数
我是一名系统分析和开发专业的学生,我正在尝试编写代码,但我被卡住了,结构和功能很容易理解,但两者都在一起工作。。。是一个。。。废话 我做错了什么在C中将参数struct传递给函数,c,function,struct,parameter-passing,C,Function,Struct,Parameter Passing,我是一名系统分析和开发专业的学生,我正在尝试编写代码,但我被卡住了,结构和功能很容易理解,但两者都在一起工作。。。是一个。。。废话 我做错了什么 #include<stdio.h> #include<stdlib.h> #include<locale.h> typedef struct mercearia { char mercadoria[20]; int quantidade; int valor; } mercearia;
#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
typedef struct mercearia
{
char mercadoria[20];
int quantidade;
int valor;
} mercearia;
void EstruturaCadastro(struct mercearia *m)
{
int i;
for(i = 0; i < 5; i++)
{
printf("\n");
setbuf(NULL, stdin);
printf("Insira nome do produto a cadastrar: ");
scanf("%s", &(*m)[i].mercadoria);
setbuf(NULL, stdin);
printf("Insira a quantidade do produto: ");
scanf("%i", &(*m)[i].quantidade);
setbuf(NULL, stdin);
printf("Insira o valor do produto: R$ ");
scanf("%i", &(*m)[i].valor);
}
}
int main(void)
{
setlocale(LC_ALL, "");
struct mercearia m[5];
EstruturaCadastro(&m);
}
#包括
#包括
#包括
类型定义结构mercearia
{
焦墨卡多里亚[20];
整数量化;
英勇;
}水星;
void estruturacadstro(结构mercearia*m)
{
int i;
对于(i=0;i<5;i++)
{
printf(“\n”);
setbuf(空,标准输入);
printf(“Insira nome do produto a地籍测量:”);
scanf(“%s”和(*m)[i].麦卡多里亚);
setbuf(空,标准输入);
printf(“产品数量信息”);
scanf(“%i”、&(*m)[i].quantidade);
setbuf(空,标准输入);
printf(“Insira o valor do produto:R$”;
scanf(“%i”、&(*m)[i].valor);
}
}
内部主(空)
{
setlocale(LC_ALL,“”);
struct mercearia m[5];
EstruturaCadastro&m;
}
它给了我一个巨大的错误列表。然后我写了同样的代码,但没有创建结构数组,代码工作得非常好。我错过了一些东西
#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
typedef struct mercearia
{
char mercadoria[20];
int quantidade;
int valor;
} mercearia;
void EstruturaCadastro(struct mercearia *x)
{
printf("\n");
printf("Insira nome do produto: ");
scanf("%s", &(*x).mercadoria);setbuf(NULL, stdin);
printf("Insira a quantidade do produto: ");
scanf("%i", &(*x).quantidade);setbuf(NULL, stdin);
printf("Insira o valor do produto: R$ ");
scanf("%i", &(*x).valor);setbuf(NULL, stdin);
}
int main(void)
{
setlocale(LC_ALL, "");
struct mercearia produto;
EstruturaCadastro(&produto);
}
#包括
#包括
#包括
类型定义结构mercearia
{
焦墨卡多里亚[20];
整数量化;
英勇;
}水星;
void estruturacadstro(结构mercearia*x)
{
printf(“\n”);
printf(“Insira nome do produto:”);
scanf(“%s”和(*x).mercadoria);setbuf(空,标准输入);
printf(“产品数量信息”);
scanf(“%i”和(*x).quantidade);setbuf(NULL,stdin);
printf(“Insira o valor do produto:R$”;
scanf(“%i”和(*x).valor);setbuf(NULL,stdin);
}
内部主(空)
{
setlocale(LC_ALL,“”);
产品结构;
EstruturaCadastro(和produto);
}
在尝试第一个示例(更难)之前,您需要理解第二个示例(简单)
无需调用setbuf()
键,按键时会刷新stdin
完成此操作后,可以在第一个示例中更改为
m[i]->mercadoria
,等等。原始函数存在各种错误。您正在向它传递一个指向struct mercearia
的指针,该指针可以在数组中传递。在函数内部,可以像对待数组一样对待m
,例如m[0]。valor
是访问第一个元素的valor
值的方式
这意味着您对scanf
的各种调用变为
scanf("%s", m[i].mercadoria);
scanf("%i", &m[i].quantidade);
scanf("%i", &m[i].valor);
请注意,与其他数据类型不同的是,对于读取字符串,您不需要传入指向字符串的指针,因此不需要前面的
&
我从不相信自己有操作符的先例,在这里我会更慷慨地使用()
:&(*m)[I].mercadoria
。这与结构无关,与如何将数组传递给函数有关。要使该代码正常工作,应将参数声明为mercearia(*m)[5]
。您可能需要使用向导。它是pittoresque。您可以对mercadoria
字段使用m[i]
而不是&(*m)[i]
,对其他两个字段使用&m[i]
而不是&(*m)[i]
。或者,您可以更改函数参数而不是参数类型。在第一个版本中通过m
而不是&m
。
scanf("%s", m[i].mercadoria);
scanf("%i", &m[i].quantidade);
scanf("%i", &m[i].valor);