Thread: Somebody please explain pointers

  1. #1
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309

    Somebody please explain pointers

    By which I mean: WHY on earth would you NEED/WANT to use a pointer? The book I'm learning from does a TERRIBLE job explaining why/when/where to use them, and every online tuturial I've read has not explained anything other than what they are. So if somebody could please explain an why/when/where to use pointers, that would be great.
    To code is divine

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Instead of passing around huge data structures to functions, etc... it's easier to just pass the location of that data structure, and then the function can go there itself. It's used in assembly, and just filtered down into C, where it is very useful.

    Although with OOP, it is usually possible to get around the need for pointers because of classes, etc... I wouldn't recommend thinking of them as useless. They're still very useful for passing arrays and C-style strings.

    Also, read the FAQ. This is a common stumbling block and gets answered all the time.

  3. #3
    Hello,

    This is a vast subject, and would be best described with links, books, and documentation.

    Here are a list of good links that can help you greater understand the use of pointers:

    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Thanks alot both of you
    To code is divine

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by Stack Overflow
    Hello,

    This is a vast subject, and would be best described with links, books, and documentation.

    Here are a list of good links that can help you greater understand the use of pointers:

    - Stack Overflow
    I would highly recommend the 'Pointers in C and C++' link. It put all of my questions about them to rest.

  6. #6
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Quote Originally Posted by homeyg
    I would highly recommend the 'Pointers in C and C++' link. It put all of my questions about them to rest.
    Read it. It mostly just gave a long definition of a pointer. I know what a poiner is what I wanted to know is why you you would need/want to use them, which I believe another one of his links answered (can't think back that far right now :P). Thank you for your imput anyways.
    To code is divine

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    With a pointer you can hold a indefined amount of data.
    Imagine you have a small program that asks the user to insert integers. If he'll insert only 2 or 3 you can declare 2 or 3 variables, but what happens if you want like 1000 numbers (poor user)? You wont declared 1000 variables. Instead you'll manipulate a pointer which points to 1000 consecutive integers.
    Other case, imagine you want to read a file and place it entirely in memory like a small text file. What will you do? You declare a pointer, then o allocate N bytes to that pointer, being N the size of the file. Then you can write to that block of memory anything you want. And to do it you only need a pointer.

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Here are two recent threads about this subject:

    Why pointers? (The answer)
    What are pointers for?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 02:57 PM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Replies: 18
    Last Post: 06-14-2003, 10:40 AM
  4. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 AM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM