Standard type "int" is a: struct ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Standard type "int" is a: struct ?

This is a challenge question. Why is a int a struct ? you could choose between struct, variable, function or class stuct was given as correct answer.

7th Sep 2017, 1:07 PM
sneeze
sneeze - avatar
4 Answers
+ 9
In short, int is a built-in datatype, which is one of the PODs (Plain Old Data). Refer: https://stackoverflow.com/questions/146452/what-are-pod-types-in-c
7th Sep 2017, 2:01 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Struct are "value" types, and classes are "reference" types. The reason you would make a certain type a struct is because: -Struct instances cannot be null. -Struct instance are copied when assigned or passed as an argument. Making int (and other "simple" types) a struct is good, because it prevents you from modifying values you are not intended to. And also it is a pretty small object, so the overhead of making a copy is also small. Consider this: int a = 2; int b = a; b++; If int was a reference type, you would have modified the value of "a" because with a reference type doing "a=b" means that a and b are the SAME object, but if it's a value type you make a copy and don't modify the original value. For more info, Google "value vs reference type" and you will learn a lot ;)
7th Sep 2017, 2:06 PM
Daniel de Lizaur
+ 1
Such a good explanation from Daniel! 👍
7th Sep 2017, 2:45 PM
Zephyr Koo
Zephyr Koo - avatar
0
I think I get it now. Is this true. So a struct is a value type, and because a int is a value type it is also a struct. And class is a reference type. I know de differences between reference types and value type. I have never thought of built-in type as being structs or classes.
8th Sep 2017, 7:47 PM
sneeze
sneeze - avatar