根据我对数据类型的理解:计算机只能存储0和1,不能存储负数、实数、字符等。所以发明了数据类型来表示(编码)这些类型数据作为一组 0 和 1,然后在需要时对其进行解码。
但是结构呢:
struct Student
{
int age;
float gpa;
};
Student s1; s1.age = 17; s1.gpa = 3.42;
Student s2; s2.age = 16; s2.gpa = 3.64;
这看起来像是一个工具,所以我不必写:
int student1_age = 17;
float student1_gpa = 3.42;
int student2_age = 16;
float student2_gpa = 3.64;
那么将 Student
视为一种数据类型是否与我们将 float
视为一种数据类型一样准确?
最佳答案
Based on my understanding of data types: computers can only store 0s and 1s, it cannot store a negative number, a real number, a character, etc. So data types were invented to represent (encode) each of these types of data as a set of 0s and 1s and then decode it when needed.
正确。例如之间的区别。 int
和 float
只是如何解释这些位(并且取决于硬件,它需要多少位)。对于负数、小数等的表示,有多种已知的可能性和处理器支持对这些数据格式的计算,但这是另一个话题。
如果定义的 struct
是一种数据类型,则取决于抽象级别(以及一些观点)。在 C 代码中,它是一种数据类型(用于定义新变量的东西);但在谈论例如时不是。 CPU 内部结构。 ...
关于c++ - 将结构视为数据类型是否准确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27588202/