Advertisement

Pointers again =-)

Started by January 28, 2002 03:27 PM
8 comments, last by Tac-Tics 22 years, 10 months ago
OK, I have a realatively decent understanding of what pointers are now, and how they work. But here''s one question I don''t understand yet. What are they actually good for (besides making arrays the fancy way)? It might be nice if someone could give me a little code snippet demonstrating some real-world use of pointers. "I''''m not evil... I just use the power of good in evil ways."
There''s plenty of topics on this exact question, one only a few days ago. Search the forums instead of making one of us do it.

Nutts

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Advertisement
I actually did do a search on it, but I didn''t find anything. I obviously didn''t look hard enough.

"I''''m not evil... I just use the power of good in evil ways."
Here's an example of changing a variable remotely.

    void main (void){  int a=2;  printf("before %d\n",a); //before changing the value of a  //give the function the address of the variable a  ChangeVarable(&a);  printf("after %d\n",a);//after changing the value remotely}void ChangeVarable(int *changed){   //the function takes the address of the variable passed and is able to change it remotely    *changed=5;}       




See? You didn't need to return anything and you didn't use any global variables. It's quite powerful.

Edited by - Nazrix on January 28, 2002 7:48:16 PM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Dynamically allocating memory:

if you want a char array with 100 elements, you'd do this, right?
char a[100];


well what if you don't know how much you'll need? Let's say in the middle of your program you want to only allocate as much as you need and you don't know how much that'll be ahead of time?

        int num;char *a;printf("enter in the number of letters in the string:\n");scanf("%d",#);a= new char[num+1]; //dynamically allocate the number of characters entered by the user (num)        


I didn't test this so please correct me if I messed up. The idea is that the variable a is the beginning of that array so you're saying "I want this much space and let a hold the beginning of that space"

I hope I was of some help.

A CRPG in development...

Need help? Well, go FAQ yourself.



Edited by - Nazrix on January 28, 2002 8:03:18 PM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Here's another example of how you can use pointers with structures or classes...

      // just a test structurestruct Test{  int blah;};void main (void){ //this dynamically allocates space for the test structure   Test *test_var=new Test;  Change(test_var);   printf("value: %d",test_var->blah);}void Change ( Test *var){// this takes in a pointer that is the type of our structure and changes it remotely  var->blah=5; //you always use -> instead of period (.) when it's a pointer of a type struct or class}      



Edited by - Nazrix on January 28, 2002 8:23:59 PM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Advertisement
pointers are wonderful..umm...thingies! now lets use some logic here. you have a function. you want to pass a huge class to it as an arguement. now what would you think to be quicker? copying the entire class? or just sending a 32 bit variable that points to it''s location?
I love me and you love you.
Thanks for all the help =-)

"I''''m not evil... I just use the power of good in evil ways."
Just a little addition to the chat :o)

you can also pass lots of function arguments into a function, all at once, by putting them inside a structure and then passing a pointer to the structure into your function....instead of passing each individual parameter in....which is actually slower (someone will almost definatley argue not that slow...bu this is games so speed is paramount)...just saves "pushing" and "popping" stuff on an off the stack...especially in tight loops...

... just a little point I like to remember...

:oD
also saves having to allocate stuff, you can allocate it only when needed, for example you don''t want you class to initialize all data on startup, only when its needed, but if those other types are complex classes, well you get the point

This topic is closed to new replies.

Advertisement