Velocity Reviews > making bytes out of bits

# making bytes out of bits

James Kuyper
Guest
Posts: n/a

 12-11-2008
George wrote:
> On Tue, 09 Dec 2008 09:44:24 -0800, Keith Thompson wrote:

> How is this expression legal:
> 5["abcdef]"

That isn't a syntactically correct expression. I suspect that you have
the last two characters reversed, and that what you're actually
referring to is 5["abcdef"].

The expression a[b] is defined as meaning *(a+b). Therefore, 5["abcdef"]
means *(5+"abcdef"). The string literal "abcdef" has a value that points
to a six-element array of char, containing the elements 'a', 'b', 'c',
'd', 'e', 'f', and '\0'. 5+"abcdef" is an expression whose value is a
pointer to char that points at the sixth element of that array.
*(5+"abcdef") dereferences that pointer, and therefore has a value of '\0'.

James Kuyper
Guest
Posts: n/a

 12-11-2008
George wrote:
> On Tue, 09 Dec 2008 18:12:31 -0800, Barry Schwarz wrote:

....
> #include <stdio.h>
>
> int main(void)
> {
> char a[40];
> char b[8][5];
>
>
> char *p1;
> char (*p2)[5];
>
> int i;
> char c;
>
> for (i=60; i = 60 + 40 ; ++ i)
> {
> a[i-60] = i;
> }

I strongly suspect that you mean "i==60+40". The way you wrote it, the
loop condition always has a value of 100, which is non-zero, so the loop
never terminates.

>> Think about the difference between p1++ and p2++.

>
> One is legal; the other is not? They vary by one degree of indirection?
> The drapes don't match the curtains?

p1++ increase the position that p1 points at by one 'char', since p1
points at a char. p2++ increases the position that p2 points at by one
array of 5 chars, since p2 points at an array of 5 chars.

George
Guest
Posts: n/a

 12-11-2008
On Wed, 10 Dec 2008 20:46:03 -0800, Barry Schwarz wrote:

> On Wed, 10 Dec 2008 18:39:34 -0700, George <(E-Mail Removed)>
> wrote:
>
>>On Tue, 09 Dec 2008 18:12:31 -0800, Barry Schwarz wrote:
>>
>>> On Tue, 9 Dec 2008 03:36:53 -0700, George <(E-Mail Removed)>
>>> wrote:
>>>

>>
>>> char a[40]
>>> char b[8][5];
>>> char *p1;
>>> char (*p2)[5];
>>> p1 = a; /* legal because a is converted to char* */
>>> p1 = b; /* illegal because b is converted to char(*)[5] */
>>> p2 = b; /* legal as noted above */
>>> p1 = (char*)b; /* legal because the cast changes the type */

>>
>>Well, I tried to populate these arrays, but I get no output:

>
> Since the compiler told you the code was broken, why did you even
> bother to execute it?
>
> And why are you repeatedly changing the topic of discussion but not
> starting a new thread. You deliberately deleted the quote from your
> message which asked the question my code tried to respond to. Your
> question had nothing to do with the you posted below.
>
>>
>>
>>
>>#include <stdio.h>
>>
>>int main(void)
>>{
>> char a[40];
>> char b[8][5];
>>
>>
>> char *p1;
>> char (*p2)[5];
>>
>>int i;
>>char c;
>>
>>for (i=60; i = 60 + 40 ; ++ i)

>
> How many times do you think this loop executes. Hint: it is not 40.
>
>>{
>>a[i-60] = i;

>
> As a result of the error above, this statement invokes undefined
> behavior.
>
>>}
>>
>>for(i = 0; i = 40; ++ i)
>>{
>>putchar(a[i]);
>>}
>>
>>putchar('\n');
>>
>>
>>
>>
>> p1 = a; /* legal because a is converted to char* */
>> p1 = b; /* illegal because b is converted to char(*)[5] */
>> p2 = b; /* legal as noted above */
>> p1 = (char*)b; /* legal because the cast
>> changes the type */
>>return 0;
>>}
>>// gcc -Wall schwartz2.c -o x.exe
>>
>>This compiles with the following warnings, but hangs.
>>
>>
>>C:\MinGW\source> gcc -Wall schwartz2.c -o x.exe

>
> I would hate to my name associated with such abominable code. But
> since you misspelled my name I don't have to worry about it.
>
>>schwartz2.c: In function `main':
>>schwartz2.c:17: warning: suggest parentheses around assignment used as
>>truth val

>
> Be honest. You are just trolling. What other reason do you have for
> ignoring the compiler identifying the problems in your code.
>
>>ue
>>schwartz2.c:22: warning: suggest parentheses around assignment used as
>>truth val
>>ue
>>schwartz2.c:33: warning: assignment from incompatible pointer type
>>schwartz2.c:15: warning: unused variable `c'
>>
>>C:\MinGW\source>
>>
>>I usually populate integer arrays by assigning them to the index of a loop
>>that runs the range. I thought it would be a good thing to avoid the first

>
> ASCII uses 0x30 and EBCDIC uses 0xf0. I once worked with a Hazeltine
> display system where '0' was 0x60 but '1' was 0x62 so it could not be
> a C system. Could you give us the brand name or the name of the
> coding convention that uses 0x3c for '0' as you claim.
>
>>
>>??
>>
>>
>>>
>>> Think about the difference between p1++ and p2++.

>>
>>One is legal; the other is not? They vary by one degree of indirection?
>>The drapes don't match the curtains?

>
> Surely you jest. Both expressions are perfectly legal if the pointer
> in question has been assigned a valid non-NULL value.
>
> Maybe you and Bill Cunningham could form a club and tutor each other.

Yes Barry, I changed the equals to less than and now have a program I
understand. Did my complete lack of snipping above appeal to your
sensibilities (rhetorical question, see number 1)?

1) Do not reply to me again.
2) Schwartz means "black".
3) If Bill's a reasonable human being, I would much rather endure his
company as opposed to yours.
4) You're the size of my average bowel movement.
--
George

Today we affirm a new commitment to live out our nation's promise through
civility, courage, compassion and character.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/

George
Guest
Posts: n/a

 12-11-2008
On Thu, 11 Dec 2008 03:07:45 GMT, James Kuyper wrote:

> George wrote:
>> On Tue, 09 Dec 2008 18:12:31 -0800, Barry Schwarz wrote:

> ...
>> #include <stdio.h>
>>
>> int main(void)
>> {
>> char a[40];
>> char b[8][5];
>>
>>
>> char *p1;
>> char (*p2)[5];
>>
>> int i;
>> char c;
>>
>> for (i=60; i = 60 + 40 ; ++ i)
>> {
>> a[i-60] = i;
>> }

>
> I strongly suspect that you mean "i==60+40". The way you wrote it, the
> loop condition always has a value of 100, which is non-zero, so the loop
> never terminates.

I got that. In the meantime, my desire to use the inferior tool which is
the C Programming Language for array manipulation has waned. Here's how
you do it in fortran:

integer, parameter :: outfile = 51
character(len=100) :: line
integer :: seqnum, eof,i, bits_needed
integer :: bit_count, ibuf, val
integer(1) :: out(100000)
character :: c*1

! main control

open(unit=50,file='george.txt',form='formatted')

! initialize counters
bit_count = 0
ibuf = 0
val = 0

line_loop: &
do
if (eof /= 0) exit line_loop
char_loop: &
do i = 1, len_trim (line)
c = line(i:i)
if (c /= '0' .and. c /= '1') cycle char_loop
! ignore non-data chars
! Pack each 0 or 1 into the next byte, order MSB first.
val = val * 2 ! shift accumulator left 1 bit
if (c == '1') val = val + 1 ! insert new bit on right
bit_count = bit_count + 1
! When next byte is complete, add to buffer.
if (bit_count == then
ibuf = ibuf + 1 ! advance buffer pointer
out(ibuf) = val ! convert for single byte output
if (val >= 12 out(ibuf) = val - 256 ! handle negs
bit_count = 0 ! reset accumulator for next byte
val = 0
end if
end do char_loop
end do line_loop

if (bit_count > 0) then
bits_needed = 8 - bit_count
val = val * (2 ** bits_needed) ! shift left, zero pad on right
ibuf = ibuf + 1 ! advance buffer pointer
out(ibuf) = val ! convert for single byte output
if (val >= 12 out(ibuf) = val - 256 ! handle negatives
end if

! Write to output file.

open (outfile, file='bin2.dat', access='direct', recl=ibuf, &
action='write', status='replace')
write (outfile, rec=1) out(1:ibuf) ! write complete buffer
close (outfile)

print *, 'Number of bytes written = ', ibuf

! clean up and exit

close(unit=50)
endprogram

! g95 dave3.f03 -o x.exe

BTW, I don't know who dave is.

>
>>> Think about the difference between p1++ and p2++.

>>
>> One is legal; the other is not? They vary by one degree of indirection?
>> The drapes don't match the curtains?

>
> p1++ increase the position that p1 points at by one 'char', since p1
> points at a char. p2++ increases the position that p2 points at by one
> array of 5 chars, since p2 points at an array of 5 chars.

Thanks. This arcane, useless, cryptic, irrelevant, stupid, parochial
formulation was brought up by Barry, with whom I've had a falling out.
I'll take this as my nod to leave. Thanks for your comment and Merry
Christmas.
--
George

The United States and our allies are determined: we refuse to live in the
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/

Phil Carmody
Guest
Posts: n/a

 12-11-2008
George <(E-Mail Removed)> writes:
> On Tue, 9 Dec 2008 06:20:14 -0800 (PST), http://www.velocityreviews.com/forums/(E-Mail Removed)
> wrote:
>
>> On 9 Dec, 10:36, George <(E-Mail Removed)> wrote:
>>> On Mon, 8 Dec 2008 23:59:31 -0800 (PST), (E-Mail Removed)
>>> wrote:

>
> [code elided and paragraphs reordered for thematic reasons]
>
>> FAQ 6.2 "But I heard that char a[] was identical to char *a"
>> then FAQs 6.3, 6.4, 6.8... well pretty much the whole section

>
> I got to that last night. It certainly makes for a better read as a hard
> copy next to a fire. Given that I'm burning books based on lack of
> relevacne to C, I think Steve's work will survive the heating season.
>
>>>> get K&R.

>>
>> read sections 5.7 and 5.9

>
> I think this is getting really close to what I'm not getting here.
>
> q1) I don't understand the first example in 5.9. B is declared as
> int *b[10];
> and b[3][4] is syntactically legal. What gives? Is twelve less than or
> equal to ten?

Where on earth did you get the 'twelve' from in the above sentence?
There is no 12 that is in any way relevant to the situation you
describe. Please get a book and learn the basics of C.

Phil
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.

Lew Pitcher
Guest
Posts: n/a

 12-11-2008
On December 11, 2008 08:56, in comp.lang.c, Phil Carmody
((E-Mail Removed)) wrote:

> George <(E-Mail Removed)> writes:

[snip]
>> q1) I don't understand the first example in 5.9. B is declared as
>> int *b[10];
>> and b[3][4] is syntactically legal. What gives? Is twelve less than or
>> equal to ten?

>
> Where on earth did you get the 'twelve' from in the above sentence?
> There is no 12 that is in any way relevant to the situation you
> describe. Please get a book and learn the basics of C.

I haven't been following this thread, but, I can see where George got 12
from, and why he asks his question.

If
b[3][4]
is syntatically legal (as george says), then it implies that the b array has
at least 12 elements (3 * 4 = 12). Actually, that addressing implies that
the array has at least 20 elements, since C uses zero-based array
subscripting (the [3] would imply that there were at least 4 major ordinal
arrays, and the [4] would imply that there were at least 5 minor ordinal
elements, and 4 * 5 = 20).

Otoh, the array is declared as
int *b[10];
giving the b array a complement of 10 elements.

Thus, the question "Is twelve less than or equal to ten?". The 12 comes from
the calculation of the minimum number of elements needed to satisfy the
b[3][4] expression, and the 10 comes from the maximum number of elements
declared for the array.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

Phil Carmody
Guest
Posts: n/a

 12-11-2008
Lew Pitcher <(E-Mail Removed)> writes:
> On December 11, 2008 08:56, in comp.lang.c, Phil Carmody
> ((E-Mail Removed)) wrote:
>
>> George <(E-Mail Removed)> writes:

> [snip]
>>> q1) I don't understand the first example in 5.9. B is declared as
>>> int *b[10];
>>> and b[3][4] is syntactically legal. What gives? Is twelve less than or
>>> equal to ten?

>>
>> Where on earth did you get the 'twelve' from in the above sentence?
>> There is no 12 that is in any way relevant to the situation you
>> describe. Please get a book and learn the basics of C.

>
> I haven't been following this thread, but, I can see where George got 12
> from, and why he asks his question.
>
> If
> b[3][4]
> is syntatically legal (as george says), then it implies that the b array has
> at least 12 elements (3 * 4 = 12).

Wrong.
int b[2][2];
foo(b[3][4]);
is *syntactically* legal. There's more to the interpretation
of computer languages than just the syntax.

> Actually, that addressing implies that
> the array has at least 20 elements, since C uses zero-based array
> subscripting (the [3] would imply that there were at least 4 major ordinal
> arrays, and the [4] would imply that there were at least 5 minor ordinal
> elements, and 4 * 5 = 20).

That's one reason why 12 is absolutely nothing to do with
the question in hand, certainly.

Wait a second, were you trying to take a contrary stance to me?
If so, why have you just supported my argument that the number 12
is irrelevant?

> Otoh, the array is declared as
> int *b[10];
> giving the b array a complement of 10 elements.
>
> Thus, the question "Is twelve less than or equal to ten?". The 12 comes from
> the calculation of the minimum number of elements needed to satisfy the
> b[3][4] expression

False, as you proved above. If you're going to contradict yourself
can you please ensure you don't accidentally agree with me with any
of the things that you say, so that way I can more easily distance

However, you're not just wrong, you're also just plain wrong.
b[3][4] can be a perfectly valid way of accessing an int even
if there's only been allocation for 5 ints.

> , and the 10 comes from the maximum number of elements
> declared for the array.

Phil
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.

jameskuyper
Guest
Posts: n/a

 12-11-2008
Lew Pitcher wrote:
> On December 11, 2008 08:56, in comp.lang.c, Phil Carmody
> ((E-Mail Removed)) wrote:
>
> > George <(E-Mail Removed)> writes:

> [snip]
> >> q1) I don't understand the first example in 5.9. B is declared as
> >> int *b[10];
> >> and b[3][4] is syntactically legal. What gives? Is twelve less than or
> >> equal to ten?

> >
> > Where on earth did you get the 'twelve' from in the above sentence?
> > There is no 12 that is in any way relevant to the situation you
> > describe. Please get a book and learn the basics of C.

>
> I haven't been following this thread, but, I can see where George got 12
> from, and why he asks his question.
>
> If
> b[3][4]
> is syntatically legal (as george says), then it implies that the b array has
> at least 12 elements (3 * 4 = 12).

As an array declaration, that would be true. Since no such declaration
appears in the program, it's completely irrelevant.

As an expression following the given declaration, b[3][4] implies
nothing of the sort. The declaration of b implies that there are 10
pointers to int. The expression b[3][4] implies that the fourth such
pointer has been set to point at an array of at least 5 integers. It
doesn't tell you whether any of the other 9 pointers have even been
set to a value, much less whether or not that value is null, and if it
it not null, you don't know anything about the length of the array
that it points at. Each pointer could point at an array of a different
length. The number 12 has no part to play in any of this.

Keith Thompson
Guest
Posts: n/a

 12-11-2008
Azazel <(E-Mail Removed)> writes:
> On 2008-12-11, Keith Thompson <(E-Mail Removed)> wrote:

[...]
>> b[3][4] obviously has two [] operators, so there are four
>> possibilities:
>>
>> 1. b is a pointer, and b[3] is also a pointer.
>> b must therefore be a pointer to pointer, such as a char**.
>> char **b;
>>
>> 2. b is a pointer to an array.
>> char *b[10];
>>
>> 3. b is an array of pointers.
>> char (*b)[10];
>>
>> 4. b is an array of arrays, i.e., a two-dimensional array.
>> char b[5][6];

>
> Are 2 & 3 correct? Surely
>
> char *b[10];
>
> is an array of 10 pointers-to-char and
>
> char (*b)[10]
>
> is a pointer to array-of-10-chars?

D'oh! You're right, of course. (I rearranged the 4 cases but failed
to rearrange the descriptions.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

James Kuyper
Guest
Posts: n/a

 12-12-2008
Joe Wright wrote:
> James Kuyper wrote:

....
>> 5["abcdef"] means *(5+"abcdef"). The string literal "abcdef" has a
>> value that points to a six-element array of char, containing the
>> elements 'a', 'b', 'c', 'd', 'e', 'f', and '\0'. 5+"abcdef" is an
>> expression whose value is a pointer to char that points at the sixth
>> element of that array. *(5+"abcdef") dereferences that pointer, and
>> therefore has a value of '\0'.

>
> You might find *(5+"abcdef") is 'f'.

Did I ever say that I knew how to count? :-{