can you store like variables in pointers?
Printable View
can you store like variables in pointers?
What do you mean like variables. Pointers are basically variables that hold memory addresses.
i mean like could you hold a variable like
int variable;
int variable2;
int variable 3;
inside of 1 pointer?
No.
You could hold all of them in one pointer one at a time but not at the same time.
well could i put the variables into structures and use pointers to call the structure?
int array[3];
array[0] = variable;
array[1] = variable2;
array[2] = variable3;
If you passed array to a function they would share a common pointer. Dynamic arrays also share a pointer.
On the other hand
int variable, variable2, variable3 = 42; // or something else
int *pointer = variable;
Right now the pointer points to variable, but it can always be reassigned.
> well could i put the variables into structures and use pointers to call the structure?
I'm going to say yes you can, but the way you phrased that is a little weird.
ya so it can?
how would i?
int *pointer = struct variable
?
im asking because my code is getting to repetitive and annoying
Code:#include <stdlib.h>
struct bucket {
int a;
int b;
int c;
};
int main(void) {
struct bucket *on_heap = malloc(sizeof(struct bucket));
on_heap->a = 42; /* for example, here we are initializing */
on_heap->b = 27;
on_heap->c = 4;
on_heap->a = 0; /* but they can be changed at any time */
/* at some point free it */
free(on_heap);
return 0;
};
> im asking because my code is getting to repetitive and annoying
Very descriptive :rolleyes:
What exactly do you "like" want to do?
for my text rpg after each turn it allows you to view your stats ,so i have keep writing it down all like 8 lines after the 80th time it gets to repetitive...so if i could just put the imformation in a struct and use the pointer to the the struct it makes 8 lines just 1
It's a pretty tall order to make it into one line. This is more of a design problem, which the structure thing can help you in. If you are clever you can make functions to automate common things that will happen to your characters:
For example if I took my bucket:
void decrease_values(struct bucket ** me);
void increase_values(struct bucket ** me);
Where in those functions, you do what took you eight lines. But since we're in the C++ forum, make characters a class, with methods and everything.
God. I had wished I knew what we were doing before.
A structure would not "like" help you decreasing much the number of lines. You would still have to output each of the members.
Use functions and the struct, like citizen suggested because it organizes your code. Don't do it because it "like" makes you write less "lines".
i swear to god if one more person does the "like"thing,i only did it once or twice
and how would i limit down my code then
> and how would i limit down my code then
You need to learn the 80/20 rule. One application is, "If your program does anything useful 80 lines of code accounts for 20% of what it does." You can not apply this literally, but as we've been saying you can not expect every application or game to work in <100 lines of code. In fact, most don't.
It was answered already. You use functions for those tasks that you repeat often.
Loops and functions.Quote:
Originally Posted by lilhawk2892
For example, write a print stat function for whenever you need to print the stats.
Or for a more OO aproach, make the stats a class, and have a print meathod.
Or overload the << operator.Something along these lines. I haven't done this in a while.Code:class myClass {
public:
std::ostream& operator << (std::ostream& os, myClass& mc) {
os << a << ' ' << b << std::endl;
return os;
}
private:
int a, b;
};
eh? WTF are you talking about? the 80/20 rule states that 80% of the execution time will be spent in 20% of the code (although in reality it's often closer to 90/10).Quote:
Originally Posted by citizen
The 80/20 rule has many applications, but I bended the principle to say what I wanted to say. I explained what I was talking about. And 90/10 is improper.
edit: I dp not intentionally give wrong advice. I'm wrong a lot. However, I simply have to explain things in a different way because thinking is hard.
>citizen:
errr its ok i didnt understand it anyways...
>mario.f:
thank you i didnt even think about that even though ive been using functions for everything else...ill make that
oh and im already up to 124 lines in about 1 minute of the game running lol.i expect easily a couple thousand XD