Thread: Me need help wit pointers, me dum dum

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    Post Me need help wit pointers, me dum dum

    Hey all,

    Can anyone give me a hand with theory behind pointers? I just don't understand them for the life of me. What is the purpose of them, what use do they have?

    The only thing I could come up with while reading tutorials and trying to understand them was that they give you an instance of a variable without actually updating the variable... lol thats so whacked, I basically mean this..

    You have Pointer A and Variable B. Variable B is set at 30, so Pointer A is set at 30. When you change Variable B to say, 29, Pointer A automatically adjusts itself to 29 as well. But I don't see much reason to that either, whats the point? You could just use Variable B and check that, rather then using a pointer.

    So I'm totally lost on what they are, can you please help me?

    Thanks a tonne,
    -Estauns

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Pointers are usually used to literlly point to another object in memory. What most people do, is use the following line of code to initialize their pointer:

    data_type *pointer_name = &variable_name[1];

    If you;re doing a teach-yourself course, don't follow their examples, because you're never going to need a point to "7", and that's why your confused. What you will need to do, is somehow pass an entire array to a function as a parameter (if you haven't learned about function paraeters yet, just keep a link to this reply or something. Instead of passing the entire array to the function, which is either impossible or impractical, depnding on how you do it. But if you set the pointe to the address of (the reason for the &) the first element in the array, then you can just pass that. Once inside the function: You can declare a new array to represent the original array inside the function. Getting the data into this new array is done this way:

    data_type array_name[size];
    // make a loop so that the following is repeated the same nmber of times as there are slots in your array, each time, have the variable i go up 1.
    array_name[i] = *pointer_name;
    pointer_name++;
    // Incrementing the pointer by 1, will set it to the next address, which will be the next slot in the array. You can either do it with a loop, or keep going until the data contained in the current slot is null. If you have other question, my email address is at the bottom.

    Sean Mackrory
    [email protected]

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    This is one of the better explanations of pointers I've found
    http://pweb.netcom.com/~tjensen/ptr/pointers.htm
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    79
    To put it short:
    a variable contains a value.
    a pointer contains a memory adress.
    using the * operator on the pointer allows you to access the value at its memory location.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    You wouldn't normally use pointers in the types of cases usually given in textbooks introducing them. Their main purpose that I've seen is to refer to large data structures with a small data structure (the pointer).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM