7

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.

aj14
  • 303
  • 1
  • 3
  • 10

4 Answers4

10

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • So why do we call a pointer a data object preferred to variable,it being a variable? – aj14 Nov 05 '16 at 18:24
  • 2
    A pointer is a data object whose value is the address of another data object. – Barmar Nov 05 '16 at 18:25
  • I am getting you a bit but still a little confused.Eg-I declare 'int a' and call 'a' a variable,I declare int *b and call 'b' a data object?In reference to your comment and mine,even variable 'a' stores a number but we still call it a variable. – aj14 Nov 05 '16 at 18:27
  • A variable is just a name. Pointers, integers, structures, characters, etc. are data. – Barmar Nov 05 '16 at 18:29
  • `b` is also a variable. The memory that `b` refers to contains a pointer, `a` contains an integer. – Barmar Nov 05 '16 at 18:30
  • 1
    Just to clarify: The term "data" is not specified by the standard; it should be avoided. There are no "data object", just "objects". Unlike other languages, which include "functions" and thus may justify a differentiation between (executable, i.e. active) code and (passive) data. – too honest for this site Nov 05 '16 at 19:09
  • @Olaf Well, I can't make up for the bad terminology used by the tutorial he's reading, so I tried to provide an answer that's meaningful in that context. – Barmar Nov 05 '16 at 19:17
  • 1
    @Barmar: I didn't blame you, just wanted to add that as clarification to the answer. I thought editing would be inappropriate and a comment the right way. Please don't understand this as criticising your text! – too honest for this site Nov 05 '16 at 19:21
6

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)
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • Functions also take up storage. But they are explicitly **no** objects. in C. And an lvalue is not necessarily modifyable (consider `const` qualified objects). – too honest for this site Nov 06 '16 at 07:12
  • Maybe the last sentence should be like : p is a pointer variable(that designates an object that stores a pointer value).Correct me if i'm wrong. – aj14 Nov 06 '16 at 12:34
-1

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

RohitS
  • 1,036
  • 12
  • 17
  • Pointers **are** objects. They are not necessarily veriables, consider `int **` pointing to some `malloc`ed object which contains pointers, but is no variable itself. Pointers are first-class types in C, exactly like `int`, etc. – too honest for this site Nov 05 '16 at 19:11
  • @Olaf i tried to generalize the language for terminology nt meaning of what pointers are...yes pointers are object – RohitS Nov 05 '16 at 19:19
  • There is noting to "generalize". The standard exactly defines what an object is and you should stick to its definitions. Correct OP if he uses missleading terminology, but don't adopt it. C uises the term "object" different from OOP, where there can be e.g. function objects. or class (resp. type) objects, too. – too honest for this site Nov 05 '16 at 19:23
  • i am not generalizing any standards ...but still i tried putting my answer in simple language that lead you to think i have changed the original context for things.. – RohitS Nov 05 '16 at 19:33
  • @Olaf also what do you mean not necessarily in your comment??then how you define pointers in general term? – RohitS Nov 05 '16 at 19:38
  • Pointers are objects. I gave you enough information , just look up in the standard. This is a language-lawyer question, so stick to it.. – too honest for this site Nov 05 '16 at 21:34
  • its still unclear to me why you are not considering my point...the answer was given considering the question no standard manipulation was intended nor will..anyways cant force anyone to take my point... – RohitS Nov 06 '16 at 06:49
-1

Data objects are the objects which stores data, the data objects in our computer are registers in Ram. Each register has an address location. Register has read_lock and write_ lock mechanism. 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. To variables read_lock and write_lock are disabled. 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; Here speed is an variable(variable data object) it's size is 4 bytes. 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. 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; 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.

chandu
  • 1