What os pointers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What os pointers

need explaination

26th Dec 2017, 10:06 AM
Nabeel Hassan
5 Answers
+ 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.
26th Dec 2017, 10:25 AM
Mustafa K.
Mustafa K. - avatar
0
@Mustafa You cannot declare pointers like "int ptr*" it leads to the compilation exception
26th Dec 2017, 11:17 AM
Jakub Stasiak
Jakub Stasiak - avatar
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; // 😔
26th Dec 2017, 11:35 AM
Alexander Kuslya
Alexander Kuslya - avatar
0
@Alexander Yes, I know. So what? It wasn't the point of my answer.
26th Dec 2017, 11:45 AM
Jakub Stasiak
Jakub Stasiak - avatar
0
@Jakub. Oh sorry i havent tried to declare it like that you said so i did not know. thanks (I edited my comment)
26th Dec 2017, 1:53 PM
Mustafa K.
Mustafa K. - avatar