Could somebody tell me what use pointers are?

Started by
8 comments, last by SSJCORY 21 years, 4 months ago
As you all probably know i am a begginer, i know a whole lot of c++ including classes functions etc, the only thing i have never used is pointers. Frankly i just don''t understand what you would use them for. I have gotten by so far without them. Are they important? Well thanks ur input would be very helpfull. Thanks. Cory Fisher
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Advertisement
there''s an article on pointers for newbies on the gamedev website. also try using www.cprograming.com and www.gametutorials.com for more info on pointers.
Try a related thread.

tcache
Contact Me
-----------
Games don''t affect kids. If Pac-man had affected us, we''d all be sitting in darkened rooms munching pills and
listening to repetitive electronic music. -- Kristian Wilson(Nintendo Inc., 1989)

tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
Pointers are usually used for altering a variable in another piece of code, but they are also used for creating a variable sized array (i.e., c style strings are pointers to characters), and are pretty much required to take advantage of virtual classes. The most interesting use in my opinion is having a pointer to a function. How could you not be impressed by a variable declared like this:

int (* PointerToFunction)(int, int) = &Function
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
A pointer is just an address that points to the start of a memory block.

So for example you want a couple of strings in your program, you can create a couple of array's, but that is not very efficient for your memmory so what you can do is dynamicly allocate memmory for the string put the string in the memmory do something with the string and free the memmory.

Example:


      void main(void){     char *ps; // Pointer to my string memmory block     char *ap; // Another pointer     sp = (char *)malloc(6); // Size of the string + 1 for the string termination character     strcpy(ps, "Hello");     printf("My string contains the following information: %s\n", ps);     ap = ps; // This copy's the memmory address from pointer ps to pointer ap     printf("My string contains the following information: %s\n", ap); // This will print the same as the first print     free(sp); // Free the memmory     // Now you have freed the memmory block where ap is pointing to (in fact sp, because you copied it) is also clear because you copy's the memmory addresses and not the data    // So for example if you try to print the data in that pointer again it will result in a crash :) because there is no data anymore}      


------

With kind regards,

Sander Aerts aka DrSnugels

Crap
---------------------------
mov ax, 013h; int 10h; mov ax, 0xa000h;
mov es, ax; mov di, 3360; mov al, 15;
stosb; mov ax, 0002h; int 21h;
mov ax, 03h; int 10h;

[edited by - Sander Aerts on December 9, 2002 12:12:58 AM]

[edited by - Sander Aerts on December 9, 2002 12:17:03 AM]
------With kind regards,Sander Aerts aka DrSnugelsCrap :)---------------------------mov ax, 013h; int 10h; mov ax, 0xa000h;mov es, ax; mov di, 3360; mov al, 15;stosb; mov ax, 0002h; int 21h;mov ax, 03h; int 10h;
Pointers r really useful for linked list and tree data structures.

An example of how a linked list can be used in a game could be:
If you had a level with hundreds of enemies, but u never know exactly how many enemies there will be at a given point in time. You could start your game with zero enemies in the level, so in this case ur linked list would be empty. As more and more enemies appear in ur level u could add these to ur linked list. When enemies die u could delete them from ur linked list. The advantage memory wise is that u never have u use more memory than u have 2.

U cuold do this as a BIG array that can hold a maximum amount of emies units, but just say ur game could have a max of 1000 bad guys at one point in time but you only have 2 because u killed them all!! then 998 units of memory would be wasted in ur array!!!
:)
who learned you to spoke?
IRC claims another helpless victim.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Pointers are also the only way to go when passing large objects around. Pointers as arguments are typically the only values copied to the call stack (4 bytes or so) as opposed to the full objects which are being pointed to (more than 4 bytes). Thus, they can make function calls more efficient.
quote:Original post by Rembrandt
Pointers are also the only way to go when passing large objects around. Pointers as arguments are typically the only values copied to the call stack (4 bytes or so) as opposed to the full objects which are being pointed to (more than 4 bytes). Thus, they can make function calls more efficient.

Try reducing usage of pointers if possible. Use references or const-references when passing into functions. Unless nullness check is required, then go for pointers. Pointers all around could shoot yourself easily.

"after many years of singularity, i'm still searching on the event horizon"

This topic is closed to new replies.

Advertisement