Having trouble understanding pointers . Could anyone please help me out ?

thanx in advance.

Having trouble understanding pointers

its not a good way for getting assistance and therefore never forget to specify the specific topic(like pointers with array , const pointer , pointer to pointer , pointer arithmetic etc.) ?

one more thing , in your case , you need a tutorial on it.What daniweb does is providing helps , it doesn't provide such a platform where one orders and another one serves , everyone here is independent to help . And they'll help where they think that the op did a lot to solve the issue but couldn't success.

what problem you are facing with pointers ?

pointer variable is such a variable that store the address of another variable.to make a variable a pointer ,you just need to add * before the name of variable thats about it.but pointers are very broad issues. however this is impossible to explain every aspect here, but i am trying to explain the basics of pointer here:

int x,y; //x and y are normal variable which'll hold integer value

int* ptr //ptr is a pointer variable which hold the address of another variable.

int *ptr_1 //ptr_1 is another pointer variable .Here the purpose is to the depict similarities between the syntaxes . i mean that int* ptr and int *ptr_1 . both are same

ptr=&x; //its initializtion of pointer. it hold the address of variable(variable x)

ptr_1=&y; //we could have done this when decalraing the variable *ptr_1. like int *ptr_1=&y;

Now , let's see the what is Dereferencing ?

int x=10;
int* ptr=&x;
cout<<*ptr;
*ptr=20;
cout<<x<<*ptr;

* is used for dereferencing . It refers to the value that is pointed by the pointer variable. in the above example , we initialize ptr vairable to the address of x. and here dereferencing ptr means getting the value of x(i.e 10).

since ptr points to x. so when we write *ptr=20 ;, this means that we are changing the content of x. now x becomes 20.

similarly , pointers can point to pointer too. this is what we callpointer to pointer. for such pointers , we need to declare them as such :

int x;
int* p=&x;     //p is pointer variable
int** pp=&p; //pp is pointer to pointer , it can hold an address of another pointer variable(p).

you can also use pointer with array elements , see the example :

int x[10];
int i;
int* ptr=x; //here x means the address of the first element in an array. and ptr is pointer to integer.
for(i=0;i<10;i++)
{
    cout<<"input value";
    cin>>x[i];
}
//here is how can we access array elements using pointer. its with pointer arithmatic
for(i=0;i<10;i++)
{
cout<<*(ptr+i)<<endl;
}

lets suppose x is stored at the memory location 1000 and also assume int to be 4 byte, ok.

int* ptr=x; if we write such , it means we are pointing to an integer, not pointing an array.i was also confused with it. but mike made this concept clear for me.

x[i] can be access using pointer as this : *(ptr+i) , because addition is commutative you can also write it as this :*(i+ptr). this is pointer arithmatic.

ptr is pointing to the address 1000. and when we write ptr+1 then it increment 4byte (because its pointing to int) , it'll not increment 1. this is the concept called pointer arithmetic.ptr+1 points to 1004 location instead of 1001 location.
now i hope , you understand the loop and therefore i don't think to explaing more about pointer arithmetic.

@OP
i am also learning on pointers , and they are not hard to learn.i'll wrtie on pointers very soon.this is very good tutorial.

@experts
please correct me , if i'm wrong.

Edited 2 Years Ago by Learner010

sorry , i thought that i was posting in C++ forum. thats why i used cout and cin.you can think of cin and cout as scanf and printf respectively.

anyways , i think that the pointer concept i explained , is also applicable to C as well as C++.

If you remember that pointer variables hold addresses, you will be well on your way.

Just remember to 'dereference' that pointer, when you wish access the value at 'that address' !!!

Pointers are an example of the use of indirect addressing.

int x = 10;
int* p_x = &x; // p_x now holds the address of an 'int' variable, the int variable x
printf( "%d\n", x ); // direct addressing
printf( "%d\n", *p_x ); // indirect addressing

Edited 2 Years Ago by David W

This article has been dead for over six months. Start a new discussion instead.