why I need to use the sizeof () operater , Pls explain the answer with a tiny c++ code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why I need to use the sizeof () operater , Pls explain the answer with a tiny c++ code

13th Jan 2017, 12:18 PM
Mohamed Ammar
Mohamed Ammar - avatar
2 Answers
+ 9
#include <iostream> using namespace std; int main() { string name = "Filip"; int number = 1234; cout<<sizeof(name)<<endl; cout<<sizeof(number); return 0; } You use sizeof() to get a number, in the case of 'string' it is the same as an array of chars, first letter is 0, and so on, so the 5 letter word returns 4 in sizeof(name)... And in the case of an integer, well it just counts how many numbers are there in an integer. That function - sizeof(smth) can be used with arrays, when you need to go through all of the elements, so instead of writing i<5 you write i<sizeof(smth).. Hope you get it
13th Jan 2017, 8:30 PM
Filip
Filip - avatar
0
Thank u , i understand it now though there still a problem and it is that i still don't know why i would need it in a real C++ application?
14th Jan 2017, 5:42 AM
Mohamed Ammar
Mohamed Ammar - avatar