0
What is the differences between * and &
3 ответов
+ 6
int n = 5;// an integer variable called n
int *ptr = &n;// a pointer variable of type integer initialized by address of n
* designate a pointer variable in this case and & indicate the address of something(&n is the address of n)
+ 3
the difference between * and & in pointers is
* sgin is used for declaring pointer in c++ program
ex. int * ptr
so int is variable datatype and * sgin must be used in declearing point because it identifies it is a pointer and ptr is it's name
and & operator is used for retrieving address of any variable
e.x
int main()
{
int a;
int *a;
a=10;
ptr=&a;
cout<<a<<endl;
cout<<ptr<<endl;
}
so the output is
10
3fx10. // it should be different in other computer because it is according to ram prodcedure the ram automatic assgin address to any thing in ram .
you can also used. & operator without declerating pointer such as
cout<<&a;
+ 1
& returns the memory address of its operand. It is applicable to objects in memory i.e variables and array elements.
when * is applied to a pointer, the pointer can access the object it points to
%p format specifier causes printf() to display the address in the format used by the host computer.
#include<stdio.h>
int main()
{
int a=5;
int *ptr=&a;
printf("%p\n",ptr);
printf("%p\n",&a);
printf("%p\n",*ptr);
*ptr=*ptr+1;
printf("%d",a);
return 0;
}
I got output like
0060FF08
0060FF08
5
6