Barry, your example would not work for a binary file.
OK, here is another example. Let's say I have got a binary file, that
contains the year and month number of today. Now, this would be written
with 0 and 1, but it would look like this: 200607.
Your description of the data format is still unclear
(to me, anyhow). Do you mean that the file contains the
number "two hundred thousand six hundred seven" as a
binary integer in the machine's native form (probably four
or eight bytes long)? What does "look like this" mean?
Ok, we would say
that one symbol occupies 4 bytes. So 2006 would be the first symbol and
07 the second.
It sounds like 07xx would be the second, where the x's
are two more bytes. What do you mean when you say 07 is
a four-byte "symbol?"
What I want to have is an array so that ptr[0] = 2006
and ptr[1] = 07.
It seems you don't realize that C supports many different
data types, and can represent 2006 in many different ways.
Some of them are
- As an int. The number two thousand six would look like
...011111010110 in the machine, where the "..." stand
for a machine-dependent number of leading zero bits.
- As another integer type: signed or unsigned long long,
long, int, short, and so on. The value would be as above,
but perhaps with more or fewer leading zeroes.
- As a float. C doesn't prescribe any particular floating-
point format, but on many machines two thousand six would
be represented as {0.9794921875 times two to the twelfth}
and might look like 01000100111110101100000000000000 if
viewed as a sequence of bits.
- As another floating-point type: double or long double.
Again, C doesn't prescribe the exact format, but it is
likely to be somewhat like that shown for float.
- As a string of four digits followed by a fifth all-zero
byte (to mark the end of the string). On many machines
this would look like 00110010 00110000 00110000 00110110
00000000 in five consecutive memory locations.
- As a pointer to a string of the form described above.
Strings are really arrays, and C cannot manipulate arrays
as freely as it handles other kinds of objects, so it is
often desirable to store the strings "elsewhere," leave
them pretty much alone, and work with pointers to them
instead. (Especially given the confusion over the "four-
byte symbol" 07 -- if the symbols actually have different
lengths, it will be cumbersome to work with them directly
as arrays.)
Let me repeat: These are only *some* of the ways you might
represent a "symbol" in a C program. Also, these are variations
on ways to represent just *one* of your "symbols;" there are
additional decisions to be made when you choose how to manage a
collection of many of them. I hope it's clear by now that simply
saying `ptr[0] = 2006' is not an adequate description of what you
are trying to accomplish; you need to be more specific.