can you store like variables in pointers?
can you store like variables in pointers?
What do you mean like variables. Pointers are basically variables that hold memory addresses.
Woop?
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.
Woop?
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
What exactly do you "like" want to do?
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
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.
Last edited by whiteflags; 07-05-2006 at 08:41 PM. Reason: Wow. I'm in the C++ forum now.
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".
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
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.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.