Thread: What are pointer really used for?

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Question What are pointer really used for?

    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.

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    if you need dynamic allocation, pointers are great...
    hasafraggin shizigishin oppashigger...

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    well...

    If you want to decide (during run-time) how many elements an array should have, pointers and dynamic allocation is the (only?) solution:
    Code:
    
    int* Array;
    Array=new int[NrOfElements];
    delete[] Array;
    
    You cannot do this:
    Code:
    
    int Array[NrOfElements];
    
    Perhaps you want to have a function that modifies the value of a local variable. Impossible without pointers:

    Code:
    
    #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;
    }
    
    There are other uses for pointers too, just have some imagination
    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.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

  5. #5
    Unregistered
    Guest
    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.

  6. #6
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    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...

  7. #7
    Unregistered
    Guest
    Well, linked lists are sort of an example of sorting by pointers.

  8. #8
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Interesting

    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.

  9. #9
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    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...

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    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.

  11. #11
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    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...

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    4
    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.

  13. #13
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    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:

    Code:
    int main()
    {
           long array[7981237893217983127893];
           return 0;
    }
    When you try to run it, you will get an error.

    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.

  14. #14
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    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...

  15. #15
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Thanks

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM