newbie program, pointers again...

M

Micah Cowan

Barry Schwarz said:
This is a popular choice. Unless you have 4GB strings, it should be
adequate.

I think you're misreading, slightly. unsigned char is more than enough
for his purposes. So is _Bool.

He might as well be doing:

puts("sizeof(char) = 1 byte(s)\n");
 
K

Keith Thompson

Martin Jørgensen said:
Thanks... Nice to know that %p needs a void*. It's easy to understand
that a pointer to int points to somewhere in memory that is size(int) =
4 bytes large, for instance. Also easy with pointer to double, - it
points to something that is size(double) = 8 bytes, at least on my computer.

A void always points to something that is 1 byte? I think I read
somebody write that sometimes char could be 2 bytes long, on some systems?

A void* pointer points to some memory location. The pointer does not
indicate the size or type of what it points to, only its location.
void* is a generic pointer type.

An int* or double* pointer, on the other hand, points to a memory
location that is (presumably) the address of an int or double object.
The type of the pointed-to object is indicated by the type of the
pointer.

Type char is always exactly one byte; that's how C defines the word
"byte". (I think this has been mentioned on this thread before.)

[...]
I read a lot of people referring to K&R...

Do yourself a favor: get a copy of it (2nd edition). H&S5 (Harbison &
Steele, 5th edition) is a very good reference (not a tutorial).
 
G

Guest

Vladimir S. Oka wrote:
-snip-
I guess, what "somebody" wanted to say is that the size of `char`
in /bits/ is not necessarily 8 (a common assumption: byte = 8 bits).
The Standard states that CHAR_BIT is _at_least_ 8. It is conceivable to
have more than 8 bits per `char`. There were machines that had 9, for
example.

Strange system... And 16 bits too for CHAR_BIT?

When you point to a location in memory, that is "void * ptr1;" and then
point to the next location, that is ptr[1] or something, then the
difference between ptr2-ptr1 is CHAR_BIT = 1 byte ?

So 1 byte = CHAR_BIT bits, that is the minimum size you can address
besides registry flags?


Best regards / Med venlig hilsen
Martin Jørgensen
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Keith said:
A void* pointer points to some memory location. The pointer does not
indicate the size or type of what it points to, only its location.
void* is a generic pointer type.

But the compiler needs to know the size for dereferencing...
An int* or double* pointer, on the other hand, points to a memory
location that is (presumably) the address of an int or double object.
The type of the pointed-to object is indicated by the type of the
pointer.

Type char is always exactly one byte; that's how C defines the word
"byte". (I think this has been mentioned on this thread before.)
Ok...



Do yourself a favor: get a copy of it (2nd edition). H&S5 (Harbison &
Steele, 5th edition) is a very good reference (not a tutorial).

Ok, thanks.


Best regards / Med venlig hilsen
Martin Jørgensen
 
V

Vladimir S. Oka

Martin Jørgensen opined:

said:
When you point to a location in memory, that is "void * ptr1;"
and then point to the next location, that is ptr[1] or
something, then the difference between ptr2-ptr1 is CHAR_BIT =
1 byte ?

You can't dereference `void *`. If you go for `char *` then the
difference is guaranteed to be 1, as sizeof(char)==1 by
definition. CHAR_BIT is number of bits in `char`, and cannot be
less than 8.
So 1 byte = CHAR_BIT bits, that is the minimum size you can
address besides registry flags?

You cannot address register flags. C knows not of registers (not
to be confused by the suggestion for the compiler to put a
variable in a register, using `register` keyword).

You can address individual bits, but only if you declare a
bitfield structure to do it. It is still not possible (in
standard, portable C) to make such a struct reside in a
processor register.
 
G

Guest

Vladimir said:
Martin Jørgensen opined:

When you point to a location in memory, that is "void * ptr1;"
and then point to the next location, that is ptr[1] or
something, then the difference between ptr2-ptr1 is CHAR_BIT =
1 byte ?


You can't dereference `void *`. If you go for `char *` then the
difference is guaranteed to be 1, as sizeof(char)==1 by
definition. CHAR_BIT is number of bits in `char`, and cannot be
less than 8.

Oh, yeah... I always forget that...
You cannot address register flags. C knows not of registers (not
to be confused by the suggestion for the compiler to put a
variable in a register, using `register` keyword).

So 1 byte = CHAR_BIT bits, that is the minimum size you can address
(that was actually what I meant, but didn't write completely)?
You can address individual bits, but only if you declare a
bitfield structure to do it. It is still not possible (in
standard, portable C) to make such a struct reside in a
processor register.


Best regards / Med venlig hilsen
Martin Jørgensen
 
V

Vladimir S. Oka

Martin Jørgensen opined:
So 1 byte = CHAR_BIT bits, that is the minimum size you can
address (that was actually what I meant, but didn't write
completely)?

Yes, because a "byte", in C, is defined as the sizeof(char), and
that is guaranteed to be 1.
 
K

Keith Thompson

Martin Jørgensen said:
When you point to a location in memory, that is "void * ptr1;" and
then point to the next location, that is ptr[1] or something, then the
difference between ptr2-ptr1 is CHAR_BIT = 1 byte ?

Not exactly. If ptr is of type void*, then the expression ptr[1] is
illegal; you can't dereference or do arithmetic on a void*. You can
convert ptr to some other pointer type and do arithmetic on it, but
then the "stride" is determined by what the other pointer type points
to.

The standard guarantees that void* and char* have the same
representation, <OT>and gcc allows arithmetic on void* as an extension
(a bad idea IMHO)</OT>, so you can sort of think of void* as a byte
pointer. But it's usually better to to think of void* as a generic
pointer type. The equivalence of void* and char* is a relic of older
versions of the language; before the C89 ANSI standard, there was no
void*, and char* served as generic pointer type.
So 1 byte = CHAR_BIT bits, that is the minimum size you can address
besides registry flags?

I'm not sure what you mean by "registry flags", but they're not
something you can access from standard C. Bit fields can be accessed,
and can be as small as 1 bit, but you can only assign to them and
refer to their values; you can't take the address of a bit field.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top