Bools and pointers | Sololearn: Learn to code for FREE!

+1

Bools and pointers

I’m trying to create a bool that returns true or false based off of an action. For instance: This Is just something to show It’s not case sensitive Void a(){ Bool b = True; Bool success = truefalse(b); //truefalse(&b)?? If (success){ //call another function } Else //do whatever } Bool truefalse(bool* x) { x = &b; If (x) { Return x; //should be true } Else return false; } The problem is that either I get a compilation error because I can’t seem to get the hand of pointers or it goes back to the other function and it returns back to false and it doesn’t work. The only way I’ve gotten my code to work andcome back true is by doing a dowhile loop in my bool function.... but I can’t get it back to false. Any help on how to do this?

7/12/2019 9:53:57 PM

mathew

15 Answers

New Answer

+1

There are several errors in here. First, you need to write bool success = truefalse(&b) because you have to pass an address. Second is in the truefalse function. The x = &b assignment is illegal since there is no variable b in the scope. You can write the function body like this if (*x) { return *x; } else return false; Although, I'm not sure what is it that you're trying to achieve since basically you don't need the truefalse function, could you elaborate it? Maybe I can help

+1

hi! thank you for replying! so basically im working with unreal engine 4. i have a couple of variables that work off each other especially the bool function that i have set up for it. basically, i have a function that reads if this button is pressed call bCallSprint (which is a bool variable) but thats when the bool function comss into play which is setSprint(bool* sprinting). when i call the first function is kicks off, it comes out to be true, but it doesnt hold the value of true. so thats why im trying to make it become a pointer so it points to the value of true and holds the value but i dont think i understand pointers at all. even when i think i do.

+1

mathew So in short, you lack of understanding regarding pointer. I'll explain it in short version. Basically a pointer is no different than a number, the only difference is that the number is an address. In example, you have this variables bool bState = true; bool* pbState = &bState; The variables are like lockers, each of the holds something and the locker itself has a number to differentiate between one locker and the other. Say that bState is a locker with the number of 42 and inside it is a state, which is true. The other locker is pbState, its number is 65, but inside the locker is number 42, which points to another locker. So, you can use &bState to obtain the bState locker number and use *pbState to obtain the number inside the locker 42.

+1

Agent_I so basically id pass around the bool* pbState around rather than the bState?

+1

mathew Sort of, I only create it to store the address. Let me give you an example bool setState(bool* b, bool state) { *b = state; return *b; } You have this variable bool var = false; You call the function like this setState(&var, true); The first argument is actually the locker number of var, not what inside it

+1

Agent_I omg!!! okay i had to reread it a couple of times but ill try right now!!

0

Agent_I okay ive tried it, it works to true, but i have set bools in the function that say bool success = setState(&var, true) if (success) { //do whatsver } else //do opposite of whatever it seems like the successs is always passing becusse im actually setting bool var to become true with the function call. i have another function that if i release the button, its supposed to reset everything to default. it works inside the reset function but when it starts to shoot back to the other functions the success check switches it back to true

0

mathew I cannot really give any solution to that since I don't know the exact problems are, unless you give me the code or at least the pseudocode

0

Agent_I okay ill send you the code! its a little log though! its not lettjng my send it all together because i think there may be a character limit

0

https://code.sololearn.com/c1BL5ftJlaT7/?ref=app Agent_I found a better way actually lol it obviously wont compile in here but if you use unreal engine, youll see what im talking about!

0

mathew Ok, this may take a while, I'm not accustomed to it yet. So you maybe don't want to wait for it, but I'll let you know if I find anyrhing. Anyway, there's a possibility that this is going to be a bit long, so maybe I will send you messages instead of posting in here, is that okay?

0

Agent_I sounds good to me! thank you so much!!

0

Agent_I after a long day of trying, i have figured if out! i had to add a couple of parameters so i could pass the value of the bool function from function to function. thank you! your idea worked a lot!

0

mathew Ok then, congrats, though I'm not sure I really helped that much, but you're welcome

0

Agent_I you helped me with undestanding pointers and i used the function you told me to use which works like a charm!