good question
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.
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:
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:#include <stdio.h> void triple(int *num) { (*num)=(*num)*3 } int main(void) { int x=5; triple(&x); printf("%d\n", x); return 0; }
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
Pretty helpful introduction. +rep
Life's too short to be cool. Be a nerd.
Good tut ! +rep
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.
Possibly the most comprehensive and excellent tutorial I have seen thus far. +rep to both
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
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.Originally Posted by dcs
Wow I changed my sig!
Nice tut dude, thanks a lot.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks