I am confused about pointer , it takes value of a vairable or only takes adress of vairable ??

Recommended Answers

All 4 Replies

A pointer is the address of a variable (or the address of an array slot, the address of a struct member etc.). For as long as the pointed-to variable is alive, you can access its value by dereferencing the pointer.

You could say that a pointer variable holds addresses ... adresses for any kind of 'object' ... including the address of an other pointer variable ... and in that case, you then have a 'pointer to a pointer'.

One very common use of pointers is when you allocate dynamic memory ... The pointer then holds the address to the first byte in that newly allocated memory.

How were you wanting to use pointers?

Can you supply some example code?

it takes value of a vairable or only takes adress of vairable ??

pointer variable takes address of another variable which must be same as pointer type.you can't point float variable using pointer to int declaration.you must have pointer to float in this case.look below :

int* ptr;
int x=10;
float y;

ptr=&y; //Incorrect . Because ptr is pointer to int and it can't be used to point float.

ptr=&x //Correct. Because Both type are same.the statement will assign the address of x to pointer ptr
print *ptr //*ptr is de referencing(i.e. get the value at address)
*ptr=20 //set the value at address .Now x becomes 20

here is how you delcare a pointer
int *ptr;
let 'a' be a variable
int a;
The statement below takes the adderess of the variable a
ptr=&a
where as the syntax given below takes the value of the variable
*ptr=a;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.