I'm not sure if this is appropriate for this kind of forum, as it is a very specific question, but I'll ask anyway.
I have three files:
main.c
functions.c
functions.h
In functions.c
and functions.h
I have created a struct
, and defined it as a type called control
:
typedef struct
{
char cUp, cLeft, cDown, cRight
} control;
I created an instance of control
, and initialized the instance called Keys
right at the beginning of main.c
:
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
int main()
{
control Keys = {'w', 'a', 's', 'd'};
//...
}
I passed Keys
into a function called options_f
, which takes a control
as its parameter and returns a control
. The function prototype is placed in functions.h
:
//in functions.h:
control options_f(control Keys);
//in main.c:
Keys = options_f(Keys);
options_f
is located in functions.c
and looks like this:
control options_f(control Keys)
{
system("cls");
printf("\nENTER | Place your symbol.\n\n1| %c |Move up.\n\n2| %c |Move left.\n\n3| %c |Move down.\n\n4| %c |Move right.\n", Keys.cUp, Keys.cLeft, Keys.cDown, Keys.cRight);
printf("\n\nTo change the keys: NUMBER,SYMBOL (enter 0 to return to menu).\n\n");
int iTemp = 0;
char cTemp = '\0';
scanf("%i,%c", &iTemp, &cTemp);
switch(iTemp)
{
case 0:
break;
case 1:
Keys.cUp = cTemp;
Keys = options_f(Keys);
break;
case 2:
Keys.cLeft = cTemp;
Keys = options_f(Keys);
break;
case 3:
Keys.cDown = cTemp;
Keys = options_f(Keys);
break;
case 4:
Keys.cRight = cTemp;
Keys = options_f(Keys);
break;
default:
printf("Please enter a number from 1-4.");
stop_f(2);
options_f(Keys);
break;
}
}
The function allows me to change the contents of the characters in Keys
. After having entered 0
and returning to the menu I called options_f
again. Only it changed the values stored in Keys
's characters to some garbage values.
Why is that?
If you need more information on other functions I used, please ask.
I'm sorry if this seems very low level and stupid to you, but I'm trying to learn, which is why I'm asking.
Thanks :)
You need to return a value from the options_f()
function (probably return Keys;
). This isn't even valid C code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.