简体   繁体   English

C语言中变量和数据对象的区别?

[英]Difference between variable and data object in C language?

I was reading C Primer Plus and came across the following lines that I couldn't understand-我正在阅读 C Primer Plus 并遇到以下我无法理解的行-

Pointers?指针? What are they?他们是什么? Basically, a pointer is a variable (or, more generally, a data object)whose value is a memory address.基本上,指针是一个变量(或者,更一般地说,是一个数据对象),其值是一个内存地址。

Just for reference,I came across these lines earlier-仅供参考,我之前遇到过这些行-

Consider an assignment statement.考虑一个赋值语句。 Its purpose is to store a value at a memory location.它的目的是在内存位置存储一个值。 Data object is a general term for a region of data storage that can be used to hold values.数据对象是可用于保存值的数据存储区域的总称。 The C standard uses just the term object for this concept. C 标准仅使用术语对象来表示此概念。 One way to identify an object is by using the name of a variable.识别对象的一种方法是使用变量的名称。

I tried googleing but couldn't find anything.These basic terminologies are confusing me so please help me understand these terms.我试过谷歌搜索但找不到任何东西。这些基本术语让我感到困惑,所以请帮助我理解这些术语。

A data object is a memory location that stores information used by the program.数据对象是存储程序使用的信息的内存位置。

A variable is a name used in the program to refer to a data object.变量是程序中用来引用数据对象的名称。

So if you write:所以如果你写:

int a;

it tells the compiler to create a data object that can hold an integer, and in the program you can use the name a to access that data object.它告诉编译器创建一个可以容纳整数的数据对象,并且在程序中您可以使用名称a来访问该数据对象。

A pointer is a data object whose value is the location in memory of some other data object.指针是一个数据对象,其值是某个其他数据对象在内存中的位置。 So if you do:所以如果你这样做:

int *pa = &a;

you're creating a variable pa that refers to a data object whose contents are the address of the data object created as a result of the a variable declaration.要创建一个变量pa是指一个数据对象,其内容是数据的地址对象创建作为的结果, a变量声明。

In C, an object is anything that takes up storage.在 C 中,对象是任何占用存储空间的东西。 C 2011 online draft : C 2011在线稿

3. Terms, definitions, and symbols 3. 术语、定义和符号
... ...
3.15 3.15
1 object 1 个对象
region of data storage in the execution environment, the contents of which can represent values 执行环境中的数据存储区域,其内容可以表示值

An lvalue is an expression that designates an object such that the contents of that object may be read or modified (basically, any expression that can be the target of an assignment is an lvalue).左值是一个表达式,它指定一个对象,以便可以读取或修改该对象的内容(基本上,任何可以作为赋值目标的表达式都是左值)。 While the C standard doesn't define the term variable , you can basically think of it as any identifier that designates an object:虽然 C 标准没有定义术语variable ,但您基本上可以将其视为指定对象的任何标识符:

int var;

The identifier var designates an object that stores an integer value;标识符var指定一个存储整数值的对象; the expression var is an lvalue, since you can read and/or modify the object through it:表达式var是一个左值,因为您可以通过它读取和/或修改对象:

var = 10;
printf( "%d\n", var );

A pointer is any expression whose value is the location of an object.指针是其值是对象位置的任何表达式。 A pointer variable is an object that stores a pointer value.指针变量是存储指针值的对象。

int *p = &var;

The identifier p designates an object that stores the location of an integer object.标识符p指定一个存储整数对象位置的对象。 The expression &var evaluates to the location (address) of the object var .表达式&var计算结果为对象var的位置(地址)。 It is not an lvalue;不是左值; it can't be the target of an assignment (you can't update an object's address).它不能是赋值的目标(你不能更新对象的地址)。 The operand of the unary & operator must be an lvalue.一元&运算符的操作数必须是左值。 The expression p , OTOH, is an lvalue since you can assign a new value to it:表达式p OTOH一个左值,因为您可以为其分配一个新值:

int y = 1;
p = &y;
printf( "%p\n", (void *) p );  // one of the few places in C you need to cast a void pointer

The expression *p designates the object that p points to (in this case, y ).表达式*p指定p指向的对象(在本例中为y )。 It is also an lvalue, since you can assign to the object through it:它也是一个左值,因为您可以通过它分配给对象:

*p = 5;  // same as y = 5
printf( "%d\n", *p );

So basically:所以基本上:

  • var , p , and y are variables (identifiers designating objects) varpy是变量(指定对象的标识符)
  • var , p , *p , and y are lvalues (expressions through which an object may be read or modified) varp*py是左值(可以读取或修改对象的表达式)
  • &var , p , &p &y are pointer expressions (expressions whose values are locations of objects) &var , p , &p &y是指针表达式(值是对象位置的表达式)
  • p is a pointer variable (object that stores a pointer value) p是指针变量(存储指针值的对象)

you already have answer but still if you want :你已经有了答案,但如果你想要:

Variables : variables can be thought as name assigned for holder that refer to your actual object so when we say int x its just like a placeholder that refer to nothing (garbage value in c) but when we say int x=10;变量:变量可以被认为是为引用您实际对象的持有者分配的名称,因此当我们说 int x 时,它就像一个不引用任何内容(c 中的垃圾值)的占位符,但是当我们说 int x=10 时; it initializes x with value 10 (now its refer to data object of type int also in this case name of data object will be maintained by compiler itself ) and we can use name x for accessing value 10.它用值 10 初始化 x(现在它引用 int 类型的数据对象,在这种情况下,数据对象的名称将由编译器本身维护),我们可以使用名称 x 来访问值 10。

Data object : as you quoted "Data object is a general term for a region of data storage that can be used to hold values."数据对象:正如您所引用的“数据对象是可用于保存值的数据存储区域的通用术语。” ie.即。 memory location记忆位置

Pointers : basically pointers are like variables which stores value but rather than storing an actual value (like int,float,char..) they hold address to memory location where your actual value is stored ie reference to data object.指针:基本上,指针就像存储值的变量,而不是存储实际值(如 int、float、char ..),它们保存存储实际值的内存位置的地址,即对数据对象的引用。

"so when you say int x you say declare a placeholder named x of type int and when you say int *x you say declare a placeholder named x of pointer type that can hold reference to an int type." “所以当你说 int x 时,你说声明一个名为 x 的占位符,当你说 int *x 时,你说声明一个名为 x 的占位符,它可以保存对 int 类型的引用。”

Data objects are the objects which stores data, the data objects in our computer are registers in Ram.数据对象是存储数据的对象,我们计算机中的数据对象是Ram中的寄存器。 Each register has an address location.每个寄存器都有一个地址位置。 Register has read_lock and write_ lock mechanism.寄存器具有 read_lock 和 write_lock 机制。 In these registers you can store variable data and constant data.在这些寄存器中,您可以存储变量数据和常量数据。 Variable data means it can modify in the entire process.可变数据意味着它可以在整个过程中修改。 Means, the Data objects which holds the variable data are called variables.意味着,保存变量数据的 Data 对象称为变量。 To variables read_lock and write_lock are disabled.变量 read_lock 和 write_lock 被禁用。 That's why you can modified the variables (variable data object) in your entire process.这就是为什么您可以在整个过程中修改变量(变量数据对象)的原因。 To identify the variable data objects you need to declare a name to it.要识别变量数据对象,您需要为其声明一个名称。 Ex: int speed: = 60;例如:int 速度:= 60; Here speed is an variable(variable data object) it's size is 4 bytes.这里的速度是一个变量(变量数据对象),它的大小是 4 个字节。 It holds the integer data only.它只保存整数数据。 The data in variables can changable in its entire application process.变量中的数据在其整个应用过程中是可变的。

In data objects if you store constant data those data objects are called contants.在数据对象中,如果您存储常量数据,则这些数据对象称为常量。 To constant data objects rd_lock is disabled but write_lock is enabled.对于常量数据对象,rd_lock 被禁用,但 write_lock 被启用。 That's why you can read the constants but can't able to assigne value to it.这就是为什么您可以读取常量但不能为其赋值的原因。 To identify constants you need to declare a name to it.要标识常量,您需要为其声明一个名称。 Ex: int const speed = 100;例如:int const speed = 100; Here speed is a constant (constant data object).这里速度是一个常数(常数数据对象)。 The data in speed can't changble in its entire application process.this speed holds data of interest type only.速度中的数据在整个应用过程中是不能改变的。这个速度只保存感兴趣类型的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM