+ Reply to Thread
Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 22
Like Tree3Likes

Thread: Pointers: What, How, and Why

  1. #11
    arkanion is offline Newbie
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Re: Pointers: What, How, and Why

    good question

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #12
    hodge-podge is offline Learning Programmer
    Join Date
    Jun 2009
    Location
    New Hampshire.
    Posts
    47
    Rep Power
    0

    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.

  4. #13
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    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.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  5. #14
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Re: Pointers: What, How, and Why

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

  6. #15
    Bartimäus's Avatar
    Bartimäus is offline Programming Expert
    Join Date
    Dec 2009
    Location
    Germany
    Posts
    490
    Rep Power
    11

    Re: Pointers: What, How, and Why

    Good tut ! +rep

  7. #16
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    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.

  8. #17
    whitey6993's Avatar
    whitey6993 is offline Programming Expert
    Join Date
    Dec 2008
    Posts
    435
    Rep Power
    15

    Re: Pointers: What, How, and Why

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

  9. #18
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    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.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  10. #19
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    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.
    Wow I changed my sig!

  11. #20
    se7en is offline Banned
    Join Date
    Nov 2009
    Posts
    30
    Rep Power
    0

    Re: Pointers: What, How, and Why

    Nice tut dude, thanks a lot.

+ Reply to Thread
Page 2 of 3 FirstFirst 123 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Pointers
    By fread in forum C and C++
    Replies: 22
    Last Post: 03-25-2010, 05:31 PM
  2. Using pointers to pointers to arrays as parameters
    By ThemePark in forum C and C++
    Replies: 6
    Last Post: 02-05-2010, 05:21 AM
  3. Pointers :@
    By Phoenixz in forum General Programming
    Replies: 4
    Last Post: 01-29-2010, 01:17 AM
  4. Pointers: why?
    By Phoenixz in forum C and C++
    Replies: 17
    Last Post: 03-21-2009, 06:11 PM
  5. What do pointers do?
    By Sionofdarkness in forum C and C++
    Replies: 15
    Last Post: 06-15-2007, 05:43 PM

Tags for this Thread

Bookmarks

Posting Permissions

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