natbob Posted August 18, 2008 Share Posted August 18, 2008 I started learning C a few days ago and have the basic syntax down. I have looked at pointers and how they work and think that I have a basic understanding of that. I have read some things stating how hard pointers were and am wondering where they would be used and why they would be so hard. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/120278-c-pointers-the-actual-thing-not-helpful-tips/ Share on other sites More sharing options...
AjBaz100 Posted August 23, 2008 Share Posted August 23, 2008 pointers aren't difficult once you get used to them and they are very powerful and extremely high performance. Quote Link to comment https://forums.phpfreaks.com/topic/120278-c-pointers-the-actual-thing-not-helpful-tips/#findComment-623507 Share on other sites More sharing options...
DarkWater Posted August 26, 2008 Share Posted August 26, 2008 Pointers allow great control of what goes where in your program. They are confusing to new programmers though, but after a while, you just "get it". It does take a lot of practice to finally use them how you want to though. Try compiling this program and running it and follow along with the output so you can kind of see how pointers function: #include <stdio.h> int main(int argc, char** argv) { float x, y; float *fp, *fp2; x = 6.5; printf("Value of x: %f\nAddress of x: %ld\n", x, &x); fp = &x; printf("Value in memory location 'fp' by deferencing: %f\n", *fp); *fp = 9.2; printf("New value of x is:\nPointer: %f\nActual Variable: %f\n", *fp, x); *fp = *fp + 1.5; y = *fp; fp2 = fp; printf("New values of:\ny: %f\nfp2: %d\n", y, fp2); } Quote Link to comment https://forums.phpfreaks.com/topic/120278-c-pointers-the-actual-thing-not-helpful-tips/#findComment-625543 Share on other sites More sharing options...
Vermillion Posted August 29, 2008 Share Posted August 29, 2008 Pointers are indeed a challenge the first time, but there's really nothing to be afraid of. While you are learning, just keep one thing in mind: Pointers allow you to pass a value by REFERENCE. Suppose you want to change the value of a variable through a function. Passing the values just like that wouldn't do the trick. Consider the following example: #include <iostream> using namespace std; void duplicate(int *p); int main(){ int a, b; a = 3; b = 4; cout << "Value of a before duplication: " << a; cout << endl << "Value of b before duplication: " << b; cout << endl << endl; duplicate(&a); duplicate(&b); cout << "Value of a after duplication: " << a; cout << endl << "value of b after duplication: " << b; cout << endl << endl; } void duplicate(int *p){ *p = *p * 2; } That will pring: Value of a before duplication: 3 Value of b before duplication: 4 Value of a after duplication: 6 Value of b after duplication: 8 If you try to pass a normal variable to the function, it is not going to work. Why? Because when you pass a variable to a function, only a copy is passed to it. The function works with they copy and then discards if. Now, if you work with a pointer, it is going to get "the address pointed at" by that pointer, and it is going to modify the variable itself. Quote Link to comment https://forums.phpfreaks.com/topic/120278-c-pointers-the-actual-thing-not-helpful-tips/#findComment-628503 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.