Difference between variable and data object in C language?

 ̄綄美尐妖づ 提交于 2019-12-07 13:46:31

问题


I was reading C Primer Plus and came across the following lines that I couldn't understand-

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. 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.


回答1:


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 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.




回答2:


In C, an object is anything that takes up storage. C 2011 online draft:

3. Terms, definitions, and symbols
...
3.15
1 object
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:

int var;

The identifier var designates an object that stores an integer value; the expression var is an lvalue, since you can read and/or modify the object through it:

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. The expression &var evaluates to the location (address) of the object 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:

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). 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)
  • var, p, *p, and y are lvalues (expressions through which an object may be read or modified)
  • &var, p, &p &y are pointer expressions (expressions whose values are locations of objects)
  • p is a pointer variable (object that stores a pointer value)



回答3:


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; 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.

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.

"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."



来源:https://stackoverflow.com/questions/40441612/difference-between-variable-and-data-object-in-c-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!