Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Feb 2003
    Location
    East Side/West Side
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts

    When to use pointers in C

    Hello,

    I recently started studying the ANSI C language and I've come accross a dilema. I read in a book that pointers are a very useful part of programming, and an example of using pointers was that you needed them to communicate with hardware (?). I don't understand why you use a pointer. In all of the examples in this book (Sams Teach Yourself C in 24 Hours) - that I've come accross - the values of the pointers can just as easily be attained by &variable. Anybody know a good reason for pointers (other than because the developer was bored)?

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Linked lists.

    That's all I've pretty much used them for in my limited, academic based usage of C++:

    template <class type>
    struct Node {
    type data;
    Node *next;
    Node(type d, Node *n) : data(d), next(n) {}
    Node(type d) : data(d), next(NULL) {}
    Node() {}
    };


    Pretty much. Languages like Java and C# don't have explicit pointers because you don't really need them, intelligent object references and you're fine.
    And pointers are prime to error, not to mention messy. A pointer is really just an integer that the comp knows points to a memory address, so you can do addition with them, and lots of other bad stuff.

  • #3
    New Coder
    Join Date
    Jun 2002
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In C I remember using pointers frequently. They bugged the heck out of me. In modern language you do not need them.

    JKD: Can't you still use pointers with ref and out in C#? I have only read a little bit on the language but I though pointers were still there.

  • #4
    Regular Coder
    Join Date
    Feb 2003
    Location
    East Side/West Side
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's what I thought...

    Good thing, because I don't really understand (or care too for that matter) pointers a whole lot, and if you don't need them, sweet

  • #5
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Originally posted by Josh Campbell
    JKD: Can't you still use pointers with ref and out in C#? I have only read a little bit on the language but I though pointers were still there.
    Well, that's a cleaner kind of pointer. ref, reference, nice stuff. Hides the dirty memory address stuff that C/C++ doesn't bother to.

  • #6
    New Coder
    Join Date
    Dec 2002
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Pointers are very useful for communicating with hardware as your book says. I've had to write some basic hardware drivers (kb/sound) and all it is is pointer manipulation.
    For one application, we had to write directly to video memory (to update the screen). How is that done? With pointers!! Pointers just hold a 32-bit (4-byte) memory address becaue there are some things that just can't be held in a variable. Let me know if you need more of an explanation, or more ways to use pointers.

    Cheers,
    Dan

  • #7
    Regular Coder
    Join Date
    Feb 2003
    Location
    East Side/West Side
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's something I'd really appreciate

    If you use MSN, and would like to get in touch with me by other means, my addy is phantasmagoria@l33ad.com


    (still, I really don't understand it a whole lot - I don't see what the difference is by using 'c' or a pointer to 'c'. Does using the actual variable slow down program execution or something?)
    Last edited by Phantom; 03-04-2003 at 02:19 AM.

  • #8
    New Coder
    Join Date
    Mar 2003
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A pointer is nothing more that a location in memory that contains the address of another location in memory. 'De-referencing' a pointer meant getting the actual address of the object the pointer points to. In Javascript, for example, objects are referred to by variable name. The variable name is actually a pointer to the location in memory where the value of the variable is kept. Most modern languages no longer require a programmer to be exactly aware where in memory data is located, thus 'pointer' is somewhat sysnonymous to the use of the word 'reference' today.
    In order to communicate with certain hardware, it is sometime necessary to create a reference or pointer to a location in memory that the hardware provides as a software location. All communication to and from the hardware is then usually handled through the pointer(s).

    A final note: Javascript is patterned after the C language. It has more abilities than stated in the usual documentation. For example, you can use curly braces to literally declare an object in Javascript although this is rarely mentioned in documentation.

    var test=
    {
    A:1,
    B:2,
    c:"a string"
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •