Could you like give me a example, a situation?
Could you like give me a example, a situation?
There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!
"...The only real game I thank in the world is baseball..." --Babe Ruth
"Life is beautiful"-Don Corleone right before he died.
"The expert on anything was once a beginner" -Baseball poster I own.
Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.
if you need dynamic allocation, pointers are great...
hasafraggin shizigishin oppashigger...
If you want to decide (during run-time) how many elements an array should have, pointers and dynamic allocation is the (only?) solution:
You cannot do this:Code:int* Array; Array=new int[NrOfElements]; delete[] Array;
Perhaps you want to have a function that modifies the value of a local variable. Impossible without pointers:Code:int Array[NrOfElements];
There are other uses for pointers too, just have some imaginationCode:#include <conio.h> #include <iostream.h> void Func(int* Var) { *Var=123; } int main() { int Variable=987; Func(&Variable); cout << Variable; //Output will be 123 getch(); return 0; }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
They can point to functions so functions can be invoked via the pointer. Very handy. They can point to anything in memory or any type of memory like EMS and XMS. In fact to use XMS you must use a pointer to it's core function to make the API calls. EMS returns the address of the page frame segment so your pointer should point to that to move pages to physical page(s) and thus to logical pages.
Pointers make it easy to associate another class to a class instead of using derived classes. This way it is easier to disassociate an interface element from a class than it would be if you use derived classes. Of course you don't get the benefits of inheritance, etc. Both methods work well in diff situations.
Pointer also allow you to swap very easily. Let's say I have a backbuffer pointer and a screen pointer - one points to offscreen mem and one points to vidram.
unsigned char *CurBuffer;
unsigned char *Screen=(unsigned char *)MK_FP(0xa000,0);
unsigned char *Buffer=new unsigned char[64000L];
If we have a bunch of video functions we would want to make them so that they can write to a provided back buffer w/o having to pass it every time they want to use the function.
void Pixel(int x,int y,char color)
{
offset=y*320+x;
CurBuffer[offset]=color;
}
#define TRUE 1
#define FALSE 0
void SetBufferMode(int mode)
{
switch (mode)
{
case TRUE: CurBuffer=Buffer;break;
case FALSE: CurBuffer=Screen;break;
}
}
This causes CurBuffer to point to Screen when buffering is off and it points to Buffer when it is on. Easy and quick.
There are a million more uses for pointers in assembly and C.
Languages that don't have 'true' pointers are lacking something.
You can use pointers, which take a small amount of memory, to manipulate large data stuctures.
For example, if you had a file with 50,000 records, each record 100 kb in size, and wanted to sort it, it would be a laborious operation to sort each separate record. Instead, sort pointers to the records. Quicker, less code, etc.
that's a really good example unregistered... a really good one... and that's a really good point bubba, a really good one... [i've for some reason never had to use either, even if the latter would seem to be more applicable...]
hasafraggin shizigishin oppashigger...
Well, linked lists are sort of an example of sorting by pointers.
This is all every interesting so the bottom line is that it IS worthwhile to learn pointers, thank you.....
There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!
"...The only real game I thank in the world is baseball..." --Babe Ruth
"Life is beautiful"-Don Corleone right before he died.
"The expert on anything was once a beginner" -Baseball poster I own.
Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.
oh yeah, that's right... blame me for never using linked lists but having ideas about what they are...
so, besides a database, what good are it's use?
hasafraggin shizigishin oppashigger...
Pointers are used to call base class virtual functions in order to call the correct derived class function at run time rather than compile time.
good point drake... which is to say that pointers can allow you to restructure your program execution sequences with minimal change... i'd say that pointer usage is to non-pointer usage like dynamic memory allocation is to static memory allocation... as an analogy...
hasafraggin shizigishin oppashigger...
If you've got a function in which a variable is created, normally this variable would be created in the stack and would be cleared after the function returns the result (or not, if it returns void). When using pointers, the variable would remain intact.
When you do something like this:
char x[823];
The array is allocated on the stack. The stack is not very large. If you do this, however:
char * x = new char[823]
...then it is allocated on the heap, and that's of just about unlimited size. (I wouldn't recommend allocated anything over what you can hold in physical memory, though).
Try to compile the following program:
When you try to run it, you will get an error.Code:int main() { long array[7981237893217983127893]; return 0; }
I figured I'd mention this because it hadn't been mentioned before. However, this is not the biggest reason to understand pointers.
Let's say, for example, that you want to make a simple text-based game where you can walk from room to room, and, when you enter a room, a pre-written description appears on the screen.
You COULD write a billion IF and switch statements. An easier way, however, would be to have a ROOM class/struct with a pointer to a character array (the description of the room) and a pointer to an array of pointers to adjacent rooms. Then you could load "maps" from files!!
I recommend that ou read the tutorial on linked lists here on CProgramming.com to get an idea of how pointers can be useful.
technically, that program wouldn't give you an error... the pointer would be NULL, and if you tried to use it, you'd get the error...
hasafraggin shizigishin oppashigger...
Thank you for the advice.
There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!
"...The only real game I thank in the world is baseball..." --Babe Ruth
"Life is beautiful"-Don Corleone right before he died.
"The expert on anything was once a beginner" -Baseball poster I own.
Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.