在 C 中,我如何评估一个函数中的局部变量,在不同的函数中?
Posted
技术标签:
【中文标题】在 C 中,我如何评估一个函数中的局部变量,在不同的函数中?【英文标题】:In C, How can I evaluate a local variable thats in one function, in a different function? 【发布时间】:2014-01-24 15:53:54 【问题描述】:好的,我正在创建这个文字冒险游戏。为了与众不同,我决定添加一个影响系统,您对其他角色的反应可以影响他们对您的反应以及他们的战斗力。 我在 C++ 方面有相当多的经验,而且我刚刚开始了第二个学期的 C 课程。到目前为止,我已经尝试过使用全局变量、结构、静态变量以及在其他函数中使用函数。我也尝试过使用指针,但每次我都会出错,所以我停止了。这是一段尝试使用影响力系统的代码(对不起星星,不想泄露任何故事情节):
#include <stdlib.h>
#include <stdio.h>
static int positive_Influence; int negative_Influence; int overall_Influence;
void printpInI(int positive_Influence, int negative_Influence);//positive and negative influence
int choice_6()
int choice = 0;
int positive_Influence, negative_Influence, overall_Influence;
positive_Influence = 0;
negative_Influence = 0;
overall_Influence = 0;
printf("What do you do?\n");
printf("\t1. You: ****\n");
printf("\t2. You: ****\n");
printf("\t3. You: ****\n");
do
scanf_s("%i", &choice);
switch (choice)
case 1:
printf("\t****?\n");
system("pause");
negative_Influence += 10;
printf("You lose influence and are now at %i with ****.\n", positive_Influence-negative_Influence);
break;
case 2:
printf("\t****");
system("pause");
positive_Influence += 10;
printf("You gain influence and are now at %i influence with ****.\n", positive_Influence-negative_Influence);
break;
case 3:
printf("**** smiles at this.\n");
system("pause");
positive_Influence += 10;
printf("You gain influence and are now at %i influence with ****.\n", positive_Influence-negative_Influence);
break;
while(choice != 1 && choice != 2 && choice != 3);
overall_Influence = positive_Influence-negative_Influence;
printf("%i\n", overall_Influence);
void story_7()
printf("Your overall influence is %i\n", overall_Influence);
int main()
choice_6();
story_7();
system("pause");
【问题讨论】:
您实际上并没有说明您的问题。我的意思是根据你的代码说你遇到的实际问题是什么。 【参考方案1】:您已将overall_influence
声明为全局变量,但也声明为choice_6
中的局部变量。本地声明优先;只需删除它,您应该就可以了。变量positive_influence
和negative_influence
也一样。
【讨论】:
【参考方案2】:您能告诉我您遇到了哪种类型的错误吗?此外,您已经声明了用于计算影响的全局变量,所以为什么您再次在 choice_6
函数中本地声明它,这是您程序中的错误情况,因此局部变量在它们声明的函数中具有比全局变量更高的优先级。所以从函数choice_6
中删除声明。
【讨论】:
【参考方案3】:实际上,我真的很愚蠢,因为我忘记了指针是如何工作的(大坝安培!)无论如何,谢谢大家的回复,我真的很感激。我还在开发一个可以使用多个敌人的战斗系统,所以请继续关注我的个人资料,因为我以后可能会有一些问题。如果您想进行 beta 测试,我的电子邮件是 mikemanahan15@gmail.com 如果您想玩我目前拥有的东西。当然,这里是完成的代码(对于choice_6 和story_7)请欣赏:
#include <stdlib.h>
#include <stdio.h>
int choice_6(int *influence)
int choice = 0;
printf("What do you do?\n");
printf("\t1. You: No. I know absolutely nothing about you. You could be a monster\n");
printf("\ttoo for all I know! Normal people don't turn into frogs!\n");
printf("\t2. You: Your right we should keep moving. You can tell me when your\n");
printf("\tready.\n");
printf("\t3. You: Okay where do you think should we go then?\n");
do
scanf_s("%i", &choice);
switch (choice)
case 1:
printf("\tBrie flashes a pained look on her face and you immediately regret saying that.\n");
printf("\tBrie: You really think I'm a monster?\n");
system("pause");
*influence -= 10;
printf("You lose influence and are now at %i with Brie.\n", *influence);
system("pause");
printf("Influence affects characters reactions towards you, their combat effectiveness, and even their allegiances in rare cases.\n");
printf("However, depending on the situation, low influence is not always a bad thing...\n");
system("pause");
printf("\tYou: Your right we should keep moving.\n");
break;
case 2:
printf("\tBrie: Thank you. I'd rather not discuss this my life story in a dark\n");
printf("\tdungeon.\n");
system("pause");
*influence += 10;
printf("You gain influence and are now at %i influence with Brie.\n", *influence);
system("pause");
printf("Influence affects characters reactions towards you, their combat effectiveness, and even their allegiances in rare cases.\n");
printf("However, depending on the situation, low influence is not always a bad thing...\n");
system("pause");
printf("\tYou: I'd have to agree with you there. Let's see what is up these stairs.\n");
choice = 2;
break;
case 3:
printf("Brie smiles at this.\n");
printf("\tBrie: Well the only way out seems to be these stairs so let's go up.\n");
system("pause");
*influence += 10;
printf("You gain influence and are now at %i influence with Brie.\n", *influence);
system("pause");
printf("Influence affects characters reactions towards you, their effectiveness in combat, and even their allegiances in rare cases.\n");
printf("However, depending on the situation, low influence is not always a bad thing...\n");
system("pause");
printf("\tYou: Sounds good to me I am quite frankly done with dungeons about now.\n");
break;
default:
printf("Type the number for the choice you want to do\n");
system("pause");
break;
while(choice != 1 && choice != 2 && choice != 3);
int story_7(int influence)
if (influence > 0)
printf("Brie laughs at this and slowly leans you off her to have both of you crouch.");
system("pause");
else
printf("Brie: Ugh just get off of me!\n");
printf("Brie pushes you off her violently and you manage to stay crouched.");
system("pause");
int main()
int influence = 0;
// ...
choice_6(&influence);
story_7(influence);
// ...
【讨论】:
【参考方案4】:您应该删除局部变量并使用范围解析运算符来使用您的全局变量 这将解决您的问题。
您还可以在以后想要的任何函数中使用这些变量。
【讨论】:
以上是关于在 C 中,我如何评估一个函数中的局部变量,在不同的函数中?的主要内容,如果未能解决你的问题,请参考以下文章