+ Reply to Thread
Page 2 of 3
FirstFirst 1 2 3 LastLast
Results 11 to 20 of 21

Thread: Pointers: What, How, and Why

  1. #11
    Newbie arkanion is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    10

    Re: Pointers: What, How, and Why

    good question

  2. #12
    Learning Programmer hodge-podge is an unknown quantity at this point
    Join Date
    Jun 2009
    Location
    New Hampshire.
    Posts
    39

    Re: Pointers: What, How, and Why

    So why should I use them? D: I don't understand why you would use them rather than just variables. Im sure there are some very good reasons, and things you can only do when using pointers, but I can't think of any.

  3. #13
    Code Warrior Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest's Avatar
    Join Date
    Sep 2009
    Location
    United States
    Age
    16
    Posts
    2,635
    Blog Entries
    2

    Re: Pointers: What, How, and Why

    There are a lot of reasons to use pointers! In fact, they are my favorite feature in C programming. If you read the tutorial, it mentions dynamic memory allocation. That means that my program can request more memory whenever I need it, which is an extremely useful feature. Also, if a function needs to modify a variable, you need to pass a pointer to it like so:
    Code:
    #include <stdio.h>
    
    void triple(int *num) {
        (*num)=(*num)*3
    }
    
    int main(void) {
        int x=5;
        triple(&x);
        printf("%d\n", x);
        return 0;
    }
    Running this program should print out 15. There are even more reasons to use pointers. You can definitely find more reasons if you do some research.
    Code:
     / ___|_   _  ___  ___| |_ 
    | |  _| | | |/ _ \/ __| __|
    | |_| | |_| |  __/\__ \ |_ 
     \____|\__,_|\___||___/\__|

  4. #14
    Programming Professional DarkLordoftheMonkeys has a spectacular aura about DarkLordoftheMonkeys has a spectacular aura about DarkLordoftheMonkeys's Avatar
    Join Date
    Oct 2009
    Location
    Massachussets
    Age
    20
    Posts
    214
    Blog Entries
    33

    Re: Pointers: What, How, and Why

    Pretty helpful introduction. +rep
    Life's too short to be cool. Be a nerd.

  5. #15
    Programming Expert Bartimäus is on a distinguished road Bartimäus's Avatar
    Join Date
    Dec 2009
    Location
    Germany
    Posts
    491

    Re: Pointers: What, How, and Why

    Good tut ! +rep

  6. #16
    dcs
    dcs is online now
    Guru dcs is just really nice dcs is just really nice dcs is just really nice dcs is just really nice
    Join Date
    Mar 2008
    Posts
    767

    Re: Pointers: What, How, and Why

    Quote Originally Posted by ZekeDragon View Post
    Gotchas:
    There are some problems with pointers that can make using them dangerous:
    1. If you attempt to dereference a pointer without assigning it a value, this results in, according to the C standard, "Undefined behavior". Undefined behavior is the worst kind of behavior, since this essentially means your entire program becomes undefined, and thus buggy. This kind of pointer is also known as a "wild pointer".
    2. Dereferencing a NULL pointer is equally dangerous, but doesn't result in undefined behavior. Instead, the C standard mandates that this results in a segmentation fault since by definition your program is broken.
    3. Obviously due to the above, passing a NULL pointer to a function that doesn't specifically document accepting NULL pointers or passing a wild pointer to any function is equally dangerous.
    The standard doesn't mention "segmentation fault". It's kinda specific that dereferencing a NULL pointer is undefined behavior, isn't it?
    If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined.84)

    [...]

    84) Thus, &*E is equivalent to E (even if E is a null pointer), and &(E1[E2]) to ((E1)+(E2)). It is always true that if E is a function designator or an lvalue that is a valid operand of the unary & operator, *&E is a function designator or an lvalue equal to E. If *P is an lvalue and T is the name of an object pointer type, *(T)P is an lvalue that has a type compatible with that to which T points.

    Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object after the end of its lifetime.

  7. #17
    Programming Expert whitey6993 has a spectacular aura about whitey6993 has a spectacular aura about whitey6993's Avatar
    Join Date
    Dec 2008
    Location
    In front of my Computer
    Posts
    408

    Re: Pointers: What, How, and Why

    Possibly the most comprehensive and excellent tutorial I have seen thus far. +rep to both

  8. #18
    Code Warrior Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest's Avatar
    Join Date
    Sep 2009
    Location
    United States
    Age
    16
    Posts
    2,635
    Blog Entries
    2

    Re: Pointers: What, How, and Why

    Quote Originally Posted by dcs View Post
    The standard doesn't mention "segmentation fault". It's kinda specific that dereferencing a NULL pointer is undefined behavior, isn't it?
    Yeah, I have to rely on Zeke to change the tutorial.
    Quote Originally Posted by whitey6993 View Post
    Possibly the most comprehensive and excellent tutorial I have seen thus far. +rep to both
    Thanks, every +rep brings me closer to world domination.
    Code:
     / ___|_   _  ___  ___| |_ 
    | |  _| | | |/ _ \/ __| __|
    | |_| | |_| |  __/\__ \ |_ 
     \____|\__,_|\___||___/\__|

  9. #19
    Moderator ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon's Avatar
    Join Date
    Jul 2009
    Location
    Nowhere, Washington
    Posts
    1,771
    Blog Entries
    40

    Re: Pointers: What, How, and Why

    Quote Originally Posted by dcs
    The standard doesn't mention "segmentation fault". It's kinda specific that dereferencing a NULL pointer is undefined behavior, isn't it?
    I researched where I got that information, and how that came about. There was quite a storm of quick research me and Guest made during this project, and it appears I simply took what Wiki said at face value instead of insisting on a direct reference from it, and misinterpreted the statement. Strange mistake, either way it's been rectified.
    Should I get a userbar here?

  10. #20
    Newbie se7en is an unknown quantity at this point se7en's Avatar
    Join Date
    Nov 2009
    Posts
    14

    Re: Pointers: What, How, and Why

    Nice tut dude, thanks a lot.

+ Reply to Thread
Page 2 of 3
FirstFirst 1 2 3 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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