• Advertisement

Archived

This topic is now archived and is closed to further replies.

Pointers again =-)

This topic is 5839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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."

Share this post


Link to post
Share on other sites
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."

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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",&num);

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

Share this post


Link to post
Share on other sites
Here's another example of how you can use pointers with structures or classes...

      

// just a test structure

struct 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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Thanks for all the help =-)

"I''''m not evil... I just use the power of good in evil ways."

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement