Jump to content

  • Log In with Google      Sign In   
  • Create Account


Pointers again =-)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Tac-Tics   Members   -  Reputation: 197

Like
Likes
Like

Posted 28 January 2002 - 09:27 AM

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

Sponsor:

#2 BeerNutts   Crossbones+   -  Reputation: 2555

Like
Likes
Like

Posted 28 January 2002 - 09:46 AM

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

#3 Tac-Tics   Members   -  Reputation: 197

Like
Likes
Like

Posted 28 January 2002 - 10:05 AM

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

#4 Nazrix   Members   -  Reputation: 307

Like
Likes
Like

Posted 28 January 2002 - 12:47 PM

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

#5 Nazrix   Members   -  Reputation: 307

Like
Likes
Like

Posted 28 January 2002 - 01:00 PM

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

#6 Nazrix   Members   -  Reputation: 307

Like
Likes
Like

Posted 28 January 2002 - 01:22 PM

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

#7 Grizwald   Members   -  Reputation: 270

Like
Likes
Like

Posted 28 January 2002 - 01:33 PM

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?

#8 Tac-Tics   Members   -  Reputation: 197

Like
Likes
Like

Posted 28 January 2002 - 10:24 PM

Thanks for all the help =-)

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

#9 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 28 January 2002 - 10:49 PM

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

#10 abdulla   Members   -  Reputation: 164

Like
Likes
Like

Posted 28 January 2002 - 10:54 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS