Re: Accessing void * buffer/array through char * pointer
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Sat, 06 Sep 2008 14:09:42 +0100
vippstar@xxxxxxxxx writes:
On Sep 6, 2:26 pm, s0s...@xxxxxxxxx wrote:<snip>
for (size_t i = 0; i < sizeof(hello); ++i) {
printf("byte %2zu: <%c>", i, p[i]);
Remove the 2 in %2zu, else the output might not be meaningful.
What on earth is wrong with the 2?
Evaluating p[i] can invoke undefined behavior.
I think it is better to say "may invoke undefined behaviour". I
accept that you don't agree (there's been a long thread about this
already) but just for the benefit of the OP there are systems on which
the code posted can't go wrong in any way. Using a potentially signed
char pointer merely limits the portability to a very specific class of
implementations and you, as the programmer, can know (with absolute
certainty) if the code's behaviour is defined or not beforehand. This
is quite unlike some other kinds of UB.
However, one should always used unsigned char for this purpose since
there is no advantage to be gained by using char *.
--
Ben.
.
- Follow-Ups:
- Re: Accessing void * buffer/array through char * pointer
- From: vippstar
- Re: Accessing void * buffer/array through char * pointer
- References:
- Accessing void * buffer/array through char * pointer
- From: s0suk3
- Re: Accessing void * buffer/array through char * pointer
- From: vippstar
- Accessing void * buffer/array through char * pointer
- Prev by Date: Re: static libirary
- Next by Date: Re: static libirary
- Previous by thread: Re: Accessing void * buffer/array through char * pointer
- Next by thread: Re: Accessing void * buffer/array through char * pointer
- Index(es):
Relevant Pages
|