Very confused with pointers

This is a discussion on Very confused with pointers within the C++ Programming forums, part of the General Programming Boards category; Im supposed to work on a program that invloves the use of pointers. Im not even going to ask about ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    17

    Very confused with pointers

    Im supposed to work on a program that invloves the use of pointers. Im not even going to ask about structs because its way beyond my understanding. But what is the point of using pointers if it just points to something else. What cant you just access that something else directly?

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Pointers do not always refer to something else. Sometimes they contain their own values. This is needed when you call many functions because variables go out of scope when you call a different function. To prevent this, one can make the variable global (which is almost as hated as goto) or use pointers.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    17
    will i be able to use a pointer in a function with out passing it in func (variable)?

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    well

    no, you cannot use pointers from main, in a function if you do not pass it. unless its global of course. pointers are needed for linked lists. pointers are nice for functions because it can modify variables in the main program if the pointer is passed with the address of that variable. also you need to know them because other things you will learn you must know pointers for.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What cant you just access that something else directly?

    Pointers are very simple when you consider that the only value the pointer holds is an address. Consider a case where you have an array of 200 strings that are 256 characters long and you want to move it around, such as passing it to a function or sorting it.

    Now, when you move that much data it's sure to be inefficient. Wouldn't it be easier to just move the address of that array instead of the array itself? When you pass the address to a function the function can still access the array simply by going to the address that you gave it.

    If you sort that array using an array of 200 addresses then you will only move the address, not the entire array. It goes without saying that an address takes up a great deal less space than a 256 character string.

    Forget about how confusing it looks and just remember that a pointer is a simple variable that holds an address, that's as hard as pointers get and once you get a feel for that one simple fact they're easy to understand.

    -Prelude
    My best code is written with the delete key.

  6. #6
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    If you have a pointer to main, and a pointer to the pointer in main that you wish to use... wouldn't you be able to use the pointer in main in the function?
    Blue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 02:57 PM
  3. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  4. confused with pointers
    By Mahdi123 in forum C Programming
    Replies: 2
    Last Post: 04-25-2007, 01:08 PM
  5. Replies: 4
    Last Post: 12-10-2006, 06:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21