Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > making bytes out of bits

Reply
Thread Tools

making bytes out of bits

 
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      12-09-2008
On 9 Dec, 10:36, George <(E-Mail Removed)> wrote:
> On Mon, 8 Dec 2008 23:59:31 -0800 (PST), (E-Mail Removed)
> wrote:
> > On 9 Dec, 03:23, George <(E-Mail Removed)> wrote:
> >> On Sun, 07 Dec 2008 23:12:24 -0800, Barry Schwarz wrote:
> >>> On Sun, 7 Dec 2008 20:36:10 -0700, George <(E-Mail Removed)>
> >>> wrote:


> >>> What does reshape arrays mean?

>
> >> Here I believe to have a char array that is x by y. *You've snipped away
> >> things I thought relevant, so let's assume they're not. *

>
> >> When you have a char array in C, you have a pointer to the the first
> >> element of this array.

>
> > no. an-array-is-not-a-pointer-a-pointer-is-not-an-array.
> > Seee the comp.lang.c FAQ for more details. really. It explains
> > it much better than I could.

>
> Your response motivated me to go to th less-heated portion of my house to
> get a hard copy of Steve Summit. *
>
> >> Maybe an eight by 5 array of chars is exactly the same as a vector of
> >> length 40 * sizeof(char)

>
> > no. They may occupy the same amount of memory but they are not
> > the same thing. C soesn't have reshapable arrays. If you want one
> > you'll have to implement it yourself. You could declare an array
> > then walk a pointer to element type over it. This might do what you
> > want.

>
> Please elaborate.
>
> <snip>


I'm not sure what you are trying to do, but this reads in
a "vector" (1-d array) and prints it as a 2-d array

#include <stdlib.h>
#include <stdio.h>

#define ROWS 8
#define COLS 5

void load_flat_array (int *a, int n)
{
int i;

for (i = 0; i < n; i++)
{
if (scanf("%d", &a[i]) != 1)
{
fprintf (stderr, "error reading number\n");
exit (EXIT_FAILURE);
}
}
}

void dump_array (int array[][COLS], int rows)
{
int i, j;

printf ("\n");

for (i = 0; i < rows; i++)
{
for (j = 0; j < COLS; j++)
printf ("%d ", array[i][j]);

printf ("\n");
}
}

int main (void)
{
int array [ROWS][COLS];

load_flat_array (&array[0][0], ROWS * COLS);
dump_array (array, ROWS);

return 0;
}


> > get K&R.


read sections 5.7 and 5.9

> Read the FAQ.
>
> It doesn't lend itself to serial reading as might a book. *Do you have a
> reference?


well you can read it serially. And you have the FAQ book which is
more
than I do.

The FAQ may be found at http://c-faq.com/

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


--
Nick Keighley
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-09-2008
Triple-DES <(E-Mail Removed)> writes:
> On 9 Des, 04:23, George <(E-Mail Removed)> wrote:

[...]
>> When you have a char array in C, you have a pointer to the the first
>> element of this array.

>
> An array is not a pointer. This is a FAQ.

[...]

George didn't actually claim otherwise, and his statement is probably
true (depending on how you interpret it).

Certainly an array is not a pointer, but:

char arr[10];

when you have a char array, you (also) have (or can trivially obtain)
a pointer to the first element of this array. In particular, the
expression ``arr'', in most contexts, evaluates to a pointer to the
first element of the array object.

I'm not sure what George *meant*, and if he doesn't already know that
an array is not a pointer he certainly needs to understand that fact
(I recommend section 6 of the comp.lang.c FAQ).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(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"
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      12-10-2008
On Tue, 9 Dec 2008 03:36:53 -0700, George <(E-Mail Removed)>
wrote:

>On Mon, 8 Dec 2008 23:59:31 -0800 (PST), (E-Mail Removed)
>wrote:
>
>> On 9 Dec, 03:23, George <(E-Mail Removed)> wrote:
>>> On Sun, 07 Dec 2008 23:12:24 -0800, Barry Schwarz wrote:
>>>> On Sun, 7 Dec 2008 20:36:10 -0700, George <(E-Mail Removed)>
>>>> wrote:

>>
>>>> What does reshape arrays mean?
>>>
>>> Here I believe to have a char array that is x by y. *You've snipped away
>>> things I thought relevant, so let's assume they're not. *
>>>
>>> When you have a char array in C, you have a pointer to the the first
>>> element of this array.

>>
>> no. an-array-is-not-a-pointer-a-pointer-is-not-an-array.
>> Seee the comp.lang.c FAQ for more details. really. It explains
>> it much better than I could.

>
>
>Your response motivated me to go to th less-heated portion of my house to
>get a hard copy of Steve Summit.
>>
>>> Maybe an eight by 5 array of chars is exactly the same as a vector of
>>> length 40 * sizeof(char)

>>
>> no. They may occupy the same amount of memory but they are not
>> the same thing. C soesn't have reshapable arrays. If you want one
>> you'll have to implement it yourself. You could declare an array
>> then walk a pointer to element type over it. This might do what you
>> want.

>
>Please elaborate.
>


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 */

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

--
Remove del for email
 
Reply With Quote
 
George
Guest
Posts: n/a
 
      12-11-2008
On Tue, 9 Dec 2008 06:20:14 -0800 (PST), (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?

> I'm not sure what you are trying to do, but this reads in
> a "vector" (1-d array) and prints it as a 2-d array


Thanks, Nick, that's a nice little program. Output:


C:\MinGW\source>x
1
2
3
4
5
6
6
6
5
4
3
3
4
5
5
6
6
6
6
6
6
5
4
34
3
3
3
3
3
3
3
3
3
3
3
3
5
56
6


7

1 2 3 4 5
6 6 6 5 4
3 3 4 5 5
6 6 6 6 6
6 5 4 34 3
3 3 3 3 3
3 3 3 3 3
3 5 56 6 7

C:\MinGW\source>

I'll see if I can adapt it.
--
George

After the chaos and carnage of September 11th, it is not enough to serve
our enemies with legal papers.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
Reply With Quote
 
George
Guest
Posts: n/a
 
      12-11-2008
On Wed, 10 Dec 2008 21:31:48 +0000, Richard Heathfield wrote:

> Barry Schwarz said:
>
>> George <(E-Mail Removed)> wrote:

>
>>> My only reference is_unleashed_ 11.

>>
>> Then get one of the other recommended references.

>
> "C Unleashed" is *not* a reference book. I agree, however, that any serious
> C programmer needs a high quality reference book.


I blew my standardized programming language reference book budget with a
fortran purchase that explains the standard chapter for chapter. It's been
invaluable. I put it on the floor and read it for the fifteen minutes that
they give you e-stim on your back before undergoing physical therapy (plus
you don't have to put your nose in that smothering place). And then
waiting for the American bus that doesn't come on time.

There are two titles for C I would buy regardless of my means:

1) The C 99 Handbook
, which does for C what my latest pruchase does for fortran, and,

2) The Essential Knuth in C

I've asked Santa for Harbison and Steele 5 for X-mas. If I were him, I
wouldn't come to New Mexico when they aren't just low-mentality,
aggressive, impatient, stupid, but drunk for the holidays as well. Just
because he can survive temperatures of 2000 degrees in chimneys doesn't
mean that he and his companions can't get run over by people who don't feel
the need to look in the direction in which they accelerate.
--
George

The true history of my administration will be written 50 years from now,
and you and I will not be around to see it.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
Reply With Quote
 
George
Guest
Posts: n/a
 
      12-11-2008
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:



#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;
}

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
schwartz2.c: In function `main':
schwartz2.c:17: warning: suggest parentheses around assignment used as
truth val
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
32 chars and start with sixty instead, which is zero on my implementation.

??


>
> 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?
--
George

States should have the right to enact laws... particularly to end the
inhumane practice of ending a life that otherwise could live.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
Reply With Quote
 
George
Guest
Posts: n/a
 
      12-11-2008
On Tue, 09 Dec 2008 09:44:24 -0800, Keith Thompson wrote:

> Triple-DES <(E-Mail Removed)> writes:


> Certainly an array is not a pointer, but:
>
> char arr[10];
>
> when you have a char array, you (also) have (or can trivially obtain)
> a pointer to the first element of this array. In particular, the
> expression ``arr'', in most contexts, evaluates to a pointer to the
> first element of the array object.
>
> I'm not sure what George *meant*, and if he doesn't already know that
> an array is not a pointer he certainly needs to understand that fact
> (I recommend section 6 of the comp.lang.c FAQ).


Yeah, I've read that now and re-recommend Steve Summit to those who need a
refresher.

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

I was thinking about how one passes an array from C as opposed to deals
with them in the syntax in which they were defined. The way I might
distinguish the array from the pointer is like unto my trade as a builder.
You have this massive structure drawn in 3-d. The way the builder lines up
the subs is the 4th dimension, and these 2 meet when a guy like me
prominently displays the address on the house.

If C builds the array, it is guaranteed to have a pointer to (0,0,0, ...)

If, however, you're given an address, then you've got to malloc space and
fill the dimensions appropriately, which is no small feat.
--
George

Any government that supports, protects or harbours terrorists is complicit
in the murder of the innocent and equally guilty of terrorist crimes.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-11-2008
George wrote:
....
> 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?


With that declaration, b is an array of 10 pointers to int. b[3] refers
to the fourth pointer in that array. b[3][4] is equivalent to *(b[3]+4),
which refers to the fourth 'int' after the one pointed at by b[3].
Whether or not such an 'int' exists and can legally be referred to
depends upon how b[3] was given it's value; it has nothing to do with
the 10 which describes the number of pointers in the array b.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-11-2008
James Kuyper <(E-Mail Removed)> writes:
> George wrote:
> ...
>> 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?

>
> With that declaration, b is an array of 10 pointers to int. b[3]
> refers to the fourth pointer in that array. b[3][4] is equivalent to
> *(b[3]+4), which refers to the fourth 'int' after the one pointed at
> by b[3]. Whether or not such an 'int' exists and can legally be
> referred to depends upon how b[3] was given it's value; it has nothing
> to do with the 10 which describes the number of pointers in the array
> b.


Right.

To elaborate a bit, the expression

b[3][4]

can actually have any of 4 different meanings, depending on how b is
declared.

The [] operator takes two operands, a pointer value and an integer
value (in either order, but we'll ignore that):
pointer_value[integer_value]
which is by definition equivalent to:
*(pointer_value + integer_value)

It's very common for the pointer value to be the result of the
implicit pointer-to-array conversion that occurs in most contexts:

double arr[10];
arr[5]; /* arr is converted from double[5] to double*,
becoming a pointer value */

But it's also common for the pointer value to be just a pointer value,
such as the value of a pointer object (which should point to the first
element of an array, but it doesn't have to be a declared array
object):

double *ptr = malloc(10 * sizeof *ptr);
if (ptr == NULL) exit(EXIT_FAILURE);
ptr[5]; /* no conversion needed, ptr is already a pointer value */

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];

In all cases involving pointers, you are responsible making sure that
the pointer points to a sufficiently large object, either by
allocating memory, by assigning it to point to a declared object, or
by some other method.

When you see the expression b[3][4] (or b[x][y], or whatever), you
can't tell which of the four cases you're dealing with unless you look
at the declaration of b. But that's not much different than x + y,
where you can't tell what kind of addition is being done without
knowing the types of x and y.

--
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"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-11-2008
George <(E-Mail Removed)> writes:
[...]
> Yeah, I've read that now and re-recommend Steve Summit to those who need a
> refresher.
>
> How is this expression legal:
> 5["abcdef]"
> ?

[...]

You've obviously read question 6.11. Have you read the answer? Or
was your question rhetorical?

--
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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shifting bits, shift 32 bits on 32 bit int GGG C++ 10 07-06-2006 06:09 AM
How do I parse certain "bits" out of bytes? Adam Schneider Perl Misc 24 05-05-2005 10:28 PM
8 bits/ch vs 16 bits/ch in PS Terry Digital Photography 5 01-21-2004 06:59 PM
8-Bits vs 12 or 16 bits/pixel; When does more than 8 bits count ? Al Dykes Digital Photography 3 12-29-2003 07:08 PM
win XP 32 bits on a 64 bits processor.. Abbyss Computer Support 3 11-13-2003 12:39 AM



Advertisments