3

I'm a beginner and I'm following a course on C programming.
In my book,a variable is defined as a memory space that stores a single unit of data (datum).
Is a data structure considered to be a single unit of data (datum)?
Ex: struct Point single_point;

3

Is a data structure considered to be a single unit of data (datum)?

Well, sometimes yes, in the very abstract, but often no if you look under the hood.

... a variable is defined as a memory space that stores a single unit of data (datum) ...

But even that single unit of data usually consists of multiple individual binary bits, maybe multiple bytes or words. So it is really a matter of terminology, or more importantly, perspective, as to whether we consider one something a datum or data. It's both is probably the most complete answer.

I think the main point I'd like to make is that there are different points of view depending on who is looking at it.

Consider the nature of the client role relative to the implementation role -- one of the most important relationships in programming. We try to separate client from implementation using abstraction; this eases programming especially as programs get larger, and change over time.

From the high-level, client point of view many items might be considered a single abstraction, and thus, logically a datum, despite consisting of multiple individual bits of state.

However, under the hood and from the implementation perspective, the datum decomposes into an organization or composition of other items.

For example, an array of int's might be considered a single datum to one way of looking at it. But it is also a composition of multiple int's. A linked list or tree is an even more complex data structure (than an array), and typically consists of multiple independent memory blocks, so it is composed of multiple data. However, we can often treat a linked list or tree as an abstraction, for example, we can pass a whole tree by reference to another function with a single parameter. Thus, from another point of view it is logically a single entity (though definitely composed of multiple words of memory).

C has union's and struct's, which reflect sum types and product types in Algebraic Data Types. These may or may not be considered data structures: however, when most use the term data structure, they are evoking a more complex (and possibly algorithmic) entity (than a single a struct or union) composed of multiple structs linked to each other by references of some sort (e.g. pointers).

Note that regarding data structures the term Abstract Data Type comes up, which need to be differentiated from Algebraic Data Type (both abbreviated ADT). Abstract data structures support the client/implementation relationship (by abstraction, of course).

There is also mereology, which is the study of whole part composition, and is a recursive concept. A car is an entity in and of itself, however, can be decomposed into its parts. Mereology is the philosophical study of the various aspects and relationships between whole and part, and as such is apropos to the question about data vs. datum.

1

Yes, a struct or more generic term, record can be considered a single unit of data. You can declare variables to hold structs and create pointers (references) to point to them.

structs are store in contiguous memory block, and you can use sizeof to get its size, making it usage very similar to primitive types.

Having said, that you can also consider a struct as grouping multiple variables together, as there are ways to access each individual variable within a struct. So in that sense, it holds multiple values.

I think the main point to understand is the C language was designed so that you can treat struct very similar to primitive types like int or char, and in that sense you can consider it as a single unit of data.

0

What you are referring to as a "data structure" is a language feature of C called a struct.

If your book defines a variable as something that stores a single unit of data, and C allows you to store a struct in a variable, then your book must define a struct as a single unit of data.

I wouldn't get too into the semantics of data, datum, a single unit of whatever. If you wanted to get into it, a single unit of data should probably be defined as one bit, but seeing as basically no system can address anything less than a byte of memory at a time then is a byte the better base unit?

It's just semantics. I would guess if you asked the person to your left he would have a different, but appropriate, definition of single unit of data to the person on your right.

0

In my book,a variable is defined as a memory space that stores a single unit of data (datum). Is a data structure considered to be a single unit of data (datum)?

Yes. C is no different than any of the other languages in that respect. A variable is nothing more than a label that refers to a block of memory by referring to the starting address in that block. In C you have two primary variable types you will run across. You have (1) a normal variable (where the label refers to the memory address holding the a value itself) and you have (2) a pointer (where the label refers to a memory address that stores the memory address to something else.) Instead of storing a value, a pointer stores the address where something else (like a value, or another pointer) can be found.

For example, if you declare:

int a = 5;

It is clear that a refers to a memory address holding the value 5. On the other hand if you declare:

int a = 5;
int *b = &a;

b is a variable that holds the address of a as its value. (the * preceding b declares b as a pointer variable.)

Since as your book states, a variable may only store a single value, then how are arrays (or struct (structures)) handled? The array variable holds the address of the first value in the array, e.g.

int a[] = { 1, 2, 3, 4, 5 };  /* declares a 5 element array */

a itself will store the address where 1 is stored in memory. (you can dereference a (e.g. *a) to verify the value pointed to is 1) A structure is no different (ignoring the automatic padding that may be inserted by the compiler) For example:

struct structa {
    int x, y, z;
};

struct structa a = { .x = 1, .y = 2, .z = 3 };

The variable a above holds the memory address where the beginning of the struct is located.

In all examples above, the variable refers to a block of memory where only a single value is stored. In the case of immediate values like 5, they hold the address where 5 is stored. In the case of compound objects like arrays or struct, etc.., the variable holds as its value the beginning address to where the array or struct is stored in memory.

0

Can a data hold multiple values?

Unless data is only a single bit it IS multiple values.

Is a data structure considered to be a single unit of data (datum)?

It depends. Oh it gloriously depends.

As the programmer, it depends on you.

You can pack as large or as little meaning into any chunk of info as you can get away with. That's actually how compression (think zip files) works.

A data structure can represent an entire computer or it can represent the C in CandiedOrange which it self is typically represented with a series of bits (usually some multiple of 8 of them) any one of which could justifiably be called a (I hate this word) datum.

However they usually aren't. The word datum (ouch) mostly comes from statistics. It likely is being used here to mean an idea.

Typical ideas you find already baked into c are normally called data types. Of these I suggest you start with studying primitives. There are many more complicated data structures, usually built out of these primitives, that can be wonderfully complex but if designed well allow you to step back and say "this all represents one idea".

Keeping to that one idea helps with many things. Not the least of which is picking a good name for the identifier/variable that points to it.

Whether anyone considers that, hopefully, one idea that your data structure represents a single datum (ew) or many datums (data darn it) depends on how closely they look at it. It should always be one idea that's made up of smaller ideas that sometimes you just don't think about.

So yeah. It depends. It really really does. Because you're going to be building these things. And they will represent what you decide they represent. If you insist on calling that a datum (ick) fine.

0

A single data item may be described by multiple attributes, each of which may potentially be a different type. For example, a person may be described by name, age, sex, height, weight, etc. A car may be described by make, model, year, color, options, etc.

A C struct type allows you to group multiple attributes together into a single object:

struct person {
  char *name;
  struct tm date_of_birth;
  char sex; 
  double height;
  double weight;
  ...
};

struct person me = { 
  "John Bode", 
  {.tm_year=<some year>, .tm_mon=<some month>, .tm_day=<some day>}, 
  'm', 
  73.0, // inches
  195.0, // lbs
  ...
};

me is a single data object, but it is composed of multiple attributes.

Notice that we can have a struct within a struct - struct tm is a type that stores datetime values broken down by year, month, day, hour, minute, second, etc. Again, we have a single data item (date or time) described by multiple attributes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.