OMG...i understand pointers!

Started by
12 comments, last by ELS1 22 years, 3 months ago
    //Quick tour of pointers because I have nothing better to do//For example purposes I'll use the data type int, //but you could use any in theoryint variable;//To Create a pointerint *pointer;//To assign a pointerpointer = &variable//To access the variable through the pointer//   *pointer//these are the same thing, really, //*pointer has the value stored by the memory at the address of//variable. Therefore *pointer==variable.//Pointers really become useful in functions.//supposing I have the following function //int Add(int x, int y)//{//   return (x+y);//}//When the function is called, the variables submitted as x and //y have to be copied to x and y so that they can be local// variables in that function.  The result also has to be copied// to the invocation of the function when it returns.  There may // well be even more to it which I've forgotten, but the point //is that an awful lot of memory that doesn't need to be used up //is ruthlessly trampled on.  How can we write some clean code //to get round this problem then?  We use pointers.//this version eliminates the need to copy things as it is using //the variables that already exist, all you are passing to this //function are the memory addresses, very useful as memory //addresses are short, but any size of data type or class or //structure, or array can be denoted by one pointer.int Add(int* x, int *y){return(*x + *y);}int main(){int a, b;a = 3;b = 4;printf("a and b add together to give, %d\n", Add(&a, &b));return 0;}    


There are other uses of pointers, but if you are currently at the stage where you don't understand pointers they probably come a little later on.

Sorry, I meant to do something short and concise but they are quite a hard topic to cover. the things you need to remember when using pointers though are some of the operators involved.

& means "Address of"
* means "value of" - known as the dereferencing operator

Any C/C++ book should explain pointers fairly well. I imagine a search of the internet could come up with some good resources fairly easily.





"In the beginning, there was nothing... which exploded." - Terry Pratchett

Edited by - hamster on January 12, 2002 1:48:18 AM
Advertisement
My favorite use for the **pointer is in function args:

  char string[] = "Whazzzup";char* ptr = NULL;void main(){ for(;;) {  IterateString(&ptr);  if(ptr)   cout << *ptr;  else   break; }}void IterateString(char** ptr){ if(*ptr == NULL)  *ptr = string; else  *ptr = *ptr + 1;}  



Stupid example, but I hope it gets the point across.



"1-2GB of virtual memory, that''s way more than i''ll ever need!" - Bill Gates
"The Adventure: Quite possibly the game of the century!" - The Gaming Community
---------------------------------------------------------------------------------------"Advances are made by answering questions. Discoveries are made by questioning answers." - Bernhard HaischReactOS
i like these pointers:

void AddToCharList( char *name, char **&list );

THAT is annoying....when you need an & to change the value, and it´s a pointer, so it gets either *& or **&
delete this;
quote:Original post by bishop_pass
Pointers are good. I enjoy both *pointers and **pointers.

However, I personally have never found a use for a ********pointer. Does anyone have any pointers with regard to this?

A sparse matrix of that order can be used for perfect chineese checkers AI.

A **pointer is called a handle is very useful. Sometimes instead of a true pointer, and index is used at the second level, but it''s still called a handle. Like a HWND.

** is also a sort of 2D matrix, and is how [][] works.


I''d go so far as to say you need pointers to code, without them it''s script.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement