Jump to content

C Pointers (the actual thing not helpful tips)


natbob

Recommended Posts

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

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.