0
What os pointers
need explaination
5 Respostas
+ 1
Pointers are the data types. They are holding the location of any other data types.
You can declare it like that:
int *p; //You are going to put '*' after data type or before variable name.
//I mean the line above can be like that:
int* p;
To find the location of data, you need to use & operator.
int x = 4;
int *ptrX;
ptrX = &x; //You wont use '*' after declaration.
now if you print ptrX:
0x04283 //you are gonna get output like that.
But if you:
cout<< *ptrX; //print like that you are gonna get the value of x
I mean, * is dereference operator as well.
When you declare variables, ram is allocating some place to your variable. For example int data types are 4 bytes and they can hold -2 billion and +2 billion.
If you declare unsigned short int, it is 2 bytes (ram is allocating place less than int) and it can hold 0 and 65k.
0
@Mustafa You cannot declare pointers like "int ptr*" it leads to the compilation exception
0
@Jakub, No, you can to define a pointer without initialization! But you can't to define a reference without initialization!
int* p; // 👌
int& r; // 😔
0
@Alexander Yes, I know. So what? It wasn't the point of my answer.
0
@Jakub. Oh sorry i havent tried to declare it like that you said so i did not know. thanks
(I edited my comment)