如何在c中传递或声明数组成员?
How to pass or declare array members in c?
我开发了一个程序,它应该通过以下函数传递双重购买数组的以下成员:
float beverages ()
{
char response;
double purchase [8]= {3.50, 3.80, 3.90, 4.20, 4.00, 4.30, 3.00, 3.10};
printf("\nEnter your order: ");
scanf("%c", response);
if (response == 'l')
{
purchase [0];
printf("You have chosen a Regular Long Black coffee\n");
printf("that will be %.2f dollars", purchase [0]);
}
else if (response == 'L')
{
purchase [1];
printf("You have chosen a Large Long Black coffee\n");
printf("that will be %.2f dollars", purchase [1]);
}
else if (response == 'f')
{
purchase [2];
printf("You have chosen a Regular Flat White coffee\n");
printf("that will be %.2f dollars", purchase [2]);
}
else if (response == 'F')
{
purchase [3];
printf ("You have chosen a Large Flat White coffee\n");
printf("that will be %.2f dollars", purchase [3]);
}
else if (response == 'c')
{
purchase [4];
printf("You have chosen a Regular Cappuccino\n");
printf("that will be %.2f dollars", purchase [4]);
}
else if (response == 'C')
{
purchase [5];
printf("You have chosen a Large Cappuccino\n");
printf("that will be %.2f dollars", purchase [5]);
}
else if (response == 't')
{
purchase [6];
printf("You have chosen a Regular Tea\n");
printf("that will be %.2f dollars", purchase [6]);
}
else if (response == 'T')
{
purchase [7];
printf("You have chosen a Large Tea\n");
printf("that will be %.2f dollars", purchase [7]);
}
return purchase[];
}
我在 int main 中有它,比如:
int main()
{
...
printf("MENU!");
decision (choice);
purchase [] = beverages();
...
return 0;
}
使用以下代码购买 [] = beverages();似乎有一个问题,编译器说 purchase 是未声明的,并且在 ] 标记之前有一个意想不到的表达式(顺便说一下,错误只在 int main 中)。似乎我没有正确地从函数传递数组或其他什么?我试过添加值 8,例如 purchase[8] = beverages();但随后编译器说 purchase 不是指针或数组。我该如何调试?我只是没有使用正确的语法还是没有正确传递数组?
因为'beverages()'函数想要returndouble array
那么你需要像
那样修改
double * beverages()
{
static double purchase [8]= {3.50, 3.80, 3.90, 4.20, 4.00, 4.30, 3.00, 3.10};
// Line of code
return purchase;
}
main函数中需要修改为
main()
{
double *purchase = beverages();
}
beverages()
中定义的变量 purchase
是该函数的局部变量,您无法在 main()
中访问它
数组 purchase 的词法范围在过程 beverages 内。您可以在 main 内部声明它,而不是尝试在 main 外部访问数组。
int main(void)
{
double purchase [8]= {3.50, 3.80, 3.90, 4.20, 4.00, 4.30, 3.00, 3.10};
...
...
}
现在您可以通过指向过程 beverages 的指针传递数组的地址
void beverages(double *purchase) {
char response;
printf("\nEnter your order: ");
scanf("%c", &response);
if (response == 'l')
{
printf("You have chosen a Regular Long Black coffee\n");
printf("that will be %.2f dollars", *purchase + 0);
}
...
}
并在main
中调用程序
int main(void)
{
double purchase [8]= {3.50, 3.80, 3.90, 4.20, 4.00, 4.30, 3.00, 3.10};
;
beverages(purchase);
}
另外如果你想改变数组的值
*(purchase + i) = value // where i is index location
我开发了一个程序,它应该通过以下函数传递双重购买数组的以下成员:
float beverages ()
{
char response;
double purchase [8]= {3.50, 3.80, 3.90, 4.20, 4.00, 4.30, 3.00, 3.10};
printf("\nEnter your order: ");
scanf("%c", response);
if (response == 'l')
{
purchase [0];
printf("You have chosen a Regular Long Black coffee\n");
printf("that will be %.2f dollars", purchase [0]);
}
else if (response == 'L')
{
purchase [1];
printf("You have chosen a Large Long Black coffee\n");
printf("that will be %.2f dollars", purchase [1]);
}
else if (response == 'f')
{
purchase [2];
printf("You have chosen a Regular Flat White coffee\n");
printf("that will be %.2f dollars", purchase [2]);
}
else if (response == 'F')
{
purchase [3];
printf ("You have chosen a Large Flat White coffee\n");
printf("that will be %.2f dollars", purchase [3]);
}
else if (response == 'c')
{
purchase [4];
printf("You have chosen a Regular Cappuccino\n");
printf("that will be %.2f dollars", purchase [4]);
}
else if (response == 'C')
{
purchase [5];
printf("You have chosen a Large Cappuccino\n");
printf("that will be %.2f dollars", purchase [5]);
}
else if (response == 't')
{
purchase [6];
printf("You have chosen a Regular Tea\n");
printf("that will be %.2f dollars", purchase [6]);
}
else if (response == 'T')
{
purchase [7];
printf("You have chosen a Large Tea\n");
printf("that will be %.2f dollars", purchase [7]);
}
return purchase[];
}
我在 int main 中有它,比如:
int main()
{
...
printf("MENU!");
decision (choice);
purchase [] = beverages();
...
return 0;
}
使用以下代码购买 [] = beverages();似乎有一个问题,编译器说 purchase 是未声明的,并且在 ] 标记之前有一个意想不到的表达式(顺便说一下,错误只在 int main 中)。似乎我没有正确地从函数传递数组或其他什么?我试过添加值 8,例如 purchase[8] = beverages();但随后编译器说 purchase 不是指针或数组。我该如何调试?我只是没有使用正确的语法还是没有正确传递数组?
因为'beverages()'函数想要returndouble array
那么你需要像
double * beverages()
{
static double purchase [8]= {3.50, 3.80, 3.90, 4.20, 4.00, 4.30, 3.00, 3.10};
// Line of code
return purchase;
}
main函数中需要修改为
main()
{
double *purchase = beverages();
}
beverages()
中定义的变量 purchase
是该函数的局部变量,您无法在 main()
数组 purchase 的词法范围在过程 beverages 内。您可以在 main 内部声明它,而不是尝试在 main 外部访问数组。
int main(void)
{
double purchase [8]= {3.50, 3.80, 3.90, 4.20, 4.00, 4.30, 3.00, 3.10};
...
...
}
现在您可以通过指向过程 beverages 的指针传递数组的地址
void beverages(double *purchase) {
char response;
printf("\nEnter your order: ");
scanf("%c", &response);
if (response == 'l')
{
printf("You have chosen a Regular Long Black coffee\n");
printf("that will be %.2f dollars", *purchase + 0);
}
...
}
并在main
中调用程序int main(void)
{
double purchase [8]= {3.50, 3.80, 3.90, 4.20, 4.00, 4.30, 3.00, 3.10};
;
beverages(purchase);
}
另外如果你想改变数组的值
*(purchase + i) = value // where i is index location