Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #78499 > unrolled thread

Ok how do I do this

Started by"Bill Cunningham" <nospam@nspam.invalid>
First post2015-12-12 17:13 -0500
Last post2015-12-13 10:26 -0800
Articles 20 on this page of 22 — 10 participants

Back to article view | Back to comp.lang.c


Contents

  Ok how do I do this "Bill Cunningham" <nospam@nspam.invalid> - 2015-12-12 17:13 -0500
    Re: Ok how do I do this Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-12-12 17:27 -0500
      Re: Ok how do I do this "Bill Cunningham" <nospam@nspam.invalid> - 2015-12-12 17:30 -0500
    Re: Ok how do I do this Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-12-12 14:40 -0800
    Re: Ok how do I do this Jerry Stuckle <jstucklex@attglobal.net> - 2015-12-12 17:55 -0500
      Re: Ok how do I do this "Bill Cunningham" <nospam@nspam.invalid> - 2015-12-12 18:01 -0500
        Re: Ok how do I do this "Osmium" <r124c4u102@comcast.net> - 2015-12-12 17:20 -0600
    Re: Ok how do I do this Keith Thompson <kst-u@mib.org> - 2015-12-12 16:34 -0800
      Re: Ok how do I do this "Bill Cunningham" <nospam@nspam.invalid> - 2015-12-12 20:51 -0500
        Re: Ok how do I do this "Osmium" <r124c4u102@comcast.net> - 2015-12-12 20:00 -0600
          Re: Ok how do I do this "Osmium" <r124c4u102@comcast.net> - 2015-12-12 20:01 -0600
        Re: Ok how do I do this Keith Thompson <kst-u@mib.org> - 2015-12-12 18:06 -0800
          Re: Ok how do I do this "Bill Cunningham" <nospam@nspam.invalid> - 2015-12-12 21:21 -0500
            Re: Ok how do I do this Keith Thompson <kst-u@mib.org> - 2015-12-12 18:41 -0800
      Re: Ok how do I do this "Bill Cunningham" <nospam@nspam.invalid> - 2015-12-12 21:12 -0500
        Re: Ok how do I do this Keith Thompson <kst-u@mib.org> - 2015-12-12 18:28 -0800
    Re: Ok how do I do this Ian Collins <ian-news@hotmail.com> - 2015-12-13 13:56 +1300
    Re: Ok how do I do this Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-12-13 15:42 +0000
      Re: Ok how do I do this "Bill Cunningham" <nospam@nspam.invalid> - 2015-12-13 11:09 -0500
        Re: Ok how do I do this "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-12-13 12:35 -0800
          Re: Ok how do I do this "Bill Cunningham" <nospam@nspam.invalid> - 2015-12-14 12:59 -0500
      Re: Ok how do I do this asetofsymbols@gmail.com - 2015-12-13 10:26 -0800

Page 1 of 2  [1] 2  Next page →


#78499 — Ok how do I do this

From"Bill Cunningham" <nospam@nspam.invalid>
Date2015-12-12 17:13 -0500
SubjectOk how do I do this
Message-ID<n4i624$det$1@dont-email.me>
     I had an unsigned int a[512] similar say to sector size  of some 
filesystems. My thinking was to use while and for anyway I ways thinking two 
loops would be needed one inside the other perhaps. To fill the array a[512] 
with the unsigned int value of 'F' which I believe is 70 in decimal. I can't 
do it. Iterrations from 0 to 511 is going to be needed and each of the 
elements a[0] for example will have to have 70 placed in there. I'm stuck. 
So I tried this function. Will it do what I want? This code is tested and 
each time I run it I get a different numeric value.

#include <stdio.h>
#include <string.h>

int main()
{
    unsigned int a[512];
    unsigned int *pu;
    pu = memset(a, 'F', 512);
    printf("%u\n", pu);
}

It this in some way doing what I want? I want a hexdump to show 512 bytes of 
F's. Or set bits. I know I could use the bitwise operators but, this might 
be a better way.

Bill

[toc] | [next] | [standalone]


#78500

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2015-12-12 17:27 -0500
Message-ID<Wa1by.31435$NY1.8356@fx35.iad>
In reply to#78499
On Saturday December 12 2015 17:13, in comp.lang.c, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

>      I had an unsigned int a[512] similar say to sector size  of some
> filesystems. My thinking was to use while and for anyway I ways thinking two
> loops would be needed one inside the other perhaps. To fill the array a[512]
> with the unsigned int value of 'F' which I believe is 70 in decimal. I can't
> do it. Iterrations from 0 to 511 is going to be needed and each of the
> elements a[0] for example will have to have 70 placed in there. I'm stuck.
> So I tried this function. Will it do what I want? This code is tested and
> each time I run it I get a different numeric value.
> 
> #include <stdio.h>
> #include <string.h>
> 
> int main()
> {
>     unsigned int a[512];
>     unsigned int *pu;
>     pu = memset(a, 'F', 512);
>     printf("%u\n", pu);
> }
> 
> It this in some way doing what I want? 

The short answer is "No, Bill. This is not 'in some way' doing what you want."

The longer answer will have to originate from some other kind respondant. Your
post and code are so wrong that it would take me more time to compose and
post a correction than I have available tonight.


-- 
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

[toc] | [prev] | [next] | [standalone]


#78501

From"Bill Cunningham" <nospam@nspam.invalid>
Date2015-12-12 17:30 -0500
Message-ID<n4i70o$h2o$1@dont-email.me>
In reply to#78500
"Lew Pitcher" <lew.pitcher@digitalfreehold.ca> wrote in message 
news:Wa1by.31435$NY1.8356@fx35.iad...

> The short answer is "No, Bill. This is not 'in some way' doing what you 
> want."
>
> The longer answer will have to originate from some other kind respondant. 
> Your
> post and code are so wrong that it would take me more time to compose and
> post a correction than I have available tonight.

    Figures, not even close. I hope anyway I am making clear what I am 
loking for.

Bill

[toc] | [prev] | [next] | [standalone]


#78502

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2015-12-12 14:40 -0800
Message-ID<62f8fa2b-367a-456b-932e-7c19a401d11f@googlegroups.com>
In reply to#78499
On Saturday, December 12, 2015 at 10:13:58 PM UTC, Bill Cunningham wrote:
> I had an unsigned int a[512] similar say to sector size  of some 
> filesystems. My thinking was to use while and for anyway I ways thinking two 
> loops would be needed one inside the other perhaps. To fill the array a[512] 
> with the unsigned int value of 'F' which I believe is 70 in decimal. I can't 
> do it. Iterrations from 0 to 511 is going to be needed and each of the 
> elements a[0] for example will have to have 70 placed in there. I'm stuck. 
> So I tried this function. Will it do what I want? This code is tested and 
> each time I run it I get a different numeric value.
> 
> #include <stdio.h>
> #include <string.h>
> 
> int main()
> {
>     unsigned int a[512];
>     unsigned int *pu;
>     pu = memset(a, 'F', 512);
>     printf("%u\n", pu);
> }
> 
> It this in some way doing what I want? I want a hexdump to show 512 bytes of 
> F's. Or set bits. I know I could use the bitwise operators but, this might 
> be a better way.
> 
> Bill

int main()
> {
>     unsigned int a[512];
>     unsigned int *pu;
      int i;
>     pu = memset(a, 'F', 512);
      for(i=0;i<512;i++)
         printf("%x\n", pu[i]);
> }

[toc] | [prev] | [next] | [standalone]


#78503

FromJerry Stuckle <jstucklex@attglobal.net>
Date2015-12-12 17:55 -0500
Message-ID<n4i8go$m65$1@dont-email.me>
In reply to#78499
On 12/12/2015 5:13 PM, Bill Cunningham wrote:
>      I had an unsigned int a[512] similar say to sector size  of some 
> filesystems. My thinking was to use while and for anyway I ways thinking two 
> loops would be needed one inside the other perhaps. To fill the array a[512] 
> with the unsigned int value of 'F' which I believe is 70 in decimal. I can't 
> do it. Iterrations from 0 to 511 is going to be needed and each of the 
> elements a[0] for example will have to have 70 placed in there. I'm stuck. 
> So I tried this function. Will it do what I want? This code is tested and 
> each time I run it I get a different numeric value.
> 
> #include <stdio.h>
> #include <string.h>
> 
> int main()
> {
>     unsigned int a[512];
>     unsigned int *pu;
>     pu = memset(a, 'F', 512);
>     printf("%u\n", pu);
> }
> 
> It this in some way doing what I want? I want a hexdump to show 512 bytes of 
> F's. Or set bits. I know I could use the bitwise operators but, this might 
> be a better way.
> 
> Bill
> 
> 

Sector size is 512 bytes (or multiple thereof).  You are doing 512 ints;
depending on the size of int on your system (16, 32 or 64 bits), your
array will be 1024, 2048 or 4096 bytes long.

Now, when you memset, you're setting the first 512 bytes of the array.
If you have a 4 byte int, the first 128 ints will contain 0xF0F0F0F0, or
4,042,322,160.  The remaining 384 ints are not set, and contain garbage.

Does this help?

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#78504

From"Bill Cunningham" <nospam@nspam.invalid>
Date2015-12-12 18:01 -0500
Message-ID<n4i8s2$nac$1@dont-email.me>
In reply to#78503
"Jerry Stuckle" <jstucklex@attglobal.net> wrote in message 
news:n4i8go$m65$1@dont-email.me...

> Sector size is 512 bytes (or multiple thereof).  You are doing 512 ints;
> depending on the size of int on your system (16, 32 or 64 bits), your
> array will be 1024, 2048 or 4096 bytes long.
>
> Now, when you memset, you're setting the first 512 bytes of the array.
> If you have a 4 byte int, the first 128 ints will contain 0xF0F0F0F0, or
> 4,042,322,160.  The remaining 384 ints are not set, and contain garbage.
>
> Does this help?

    Yes it certainly does. Some kind of generic type might be needed. Chars 
on my machine are 8 bit. Most computers, PCs anyway consider 8 bit a byte. I 
see this will not be what I need. :(

Bill

[toc] | [prev] | [next] | [standalone]


#78505

From"Osmium" <r124c4u102@comcast.net>
Date2015-12-12 17:20 -0600
Message-ID<dd3oe3F1bg2U1@mid.individual.net>
In reply to#78504
"Bill Cunningham" wrote:

> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message 
> news:n4i8go$m65$1@dont-email.me...
>
>> Sector size is 512 bytes (or multiple thereof).  You are doing 512 ints;
>> depending on the size of int on your system (16, 32 or 64 bits), your
>> array will be 1024, 2048 or 4096 bytes long.
>>
>> Now, when you memset, you're setting the first 512 bytes of the array.
>> If you have a 4 byte int, the first 128 ints will contain 0xF0F0F0F0, or
>> 4,042,322,160.  The remaining 384 ints are not set, and contain garbage.
>>
>> Does this help?
>
>    Yes it certainly does. Some kind of generic type might be needed. Chars 
> on my machine are 8 bit. Most computers, PCs anyway consider 8 bit a byte. 
> I see this will not be what I need. :(

If you can write your program in terms of 8-bit char there will be no 
possibility of endian problems. The larger sizes open up that damned can of 
worms. 

[toc] | [prev] | [next] | [standalone]


#78506

FromKeith Thompson <kst-u@mib.org>
Date2015-12-12 16:34 -0800
Message-ID<lnbn9vgp9v.fsf@kst-u.example.com>
In reply to#78499
"Bill Cunningham" <nospam@nspam.invalid> writes:
>      I had an unsigned int a[512] similar say to sector size  of some 
> filesystems. My thinking was to use while and for anyway I ways thinking two 
> loops would be needed one inside the other perhaps. To fill the array a[512] 
> with the unsigned int value of 'F' which I believe is 70 in decimal. I can't 
> do it. Iterrations from 0 to 511 is going to be needed and each of the 
> elements a[0] for example will have to have 70 placed in there. I'm stuck. 
> So I tried this function. Will it do what I want? This code is tested and 
> each time I run it I get a different numeric value.
>
> #include <stdio.h>
> #include <string.h>
>
> int main()
> {
>     unsigned int a[512];
>     unsigned int *pu;
>     pu = memset(a, 'F', 512);
>     printf("%u\n", pu);
> }
>
> It this in some way doing what I want? I want a hexdump to show 512 bytes of 
> F's. Or set bits. I know I could use the bitwise operators but, this might 
> be a better way.

You say you know you could use the bitwise operators.  I don't see how.

You say you want 512 *bytes* of 'F's, but your array is 512*sizeof(int)
bytes, not 512 bytes.  If sizeof(unsigned int)==4, as is typical, then
your array is 2048 bytes.  sizeof(unsigned int) can be larger or smaller
than 4 on different systems.

Yes, 'F' == 70 in ASCII.

"int main()" is better written as "int main(void)".  (This is a minor
point; "int main()" will also work.)

Do you want each of the 512 unsigned int elements to hold the value 'F'
or do you want each of the (probably) 2048 bytes of the array to hold
the value 'F'?  Neither makes a whole lot of sense, so I can't guess
which one you want.  If you want to store character values, you should
probably use a character type, for example:
    unsigned char a[512];
(Or I suppose you could set just the first 512 bytes of your 2048-byte
array to 'F', which is what your current code does, but that makes even
less sense.)

Let's assume sizeof (unsigned int) == 4, which is typical.  70 in
hexadecimal is 0x46.  Do you want each element of your array to hold the
value 0x00000046, or 0x46464646?  (This is a rephrasing of the same
question I asked in the previous paragraph.)

memset() returns the value of its first argument.  It has no way to
indicate an error.  So there's no point in storing the value in another
variable.  You can drop pu.

Your printf call
    printf("%u\n", pu);
almost certainly doesn't do what you want.  It's an incorrect way of
printing the *address* of (the beginning of) your array.  The correct
way to print that address would be any of the following:
    printf("%p\n", (void*)pu);
    printf("%p\n", (void*)a);
    printf("%p\n", (void*)&a);
    printf("%p\n", (void*)&a[0]);

To set each byte of the array to 'F' (thereby setting each element to
0x46464646):

    memset(a, 'F', sizeof a);

To set each element of the array to 'F' (0x00000046):

    for (int i = 0; i < sizeof a / sizeof a[0]; i ++) {
        a[i] = 'F';
    }

In that case you can drop the "#include <string.h>".  (The memset
function sets a sequence of bytes.  There is no corresponding function
to set a sequence of unsigned ints, so you have to write your own loop.)

Printing the stored value of `a` is left as an exercise.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#78512

From"Bill Cunningham" <nospam@nspam.invalid>
Date2015-12-12 20:51 -0500
Message-ID<n4iipp$p6o$1@dont-email.me>
In reply to#78506
"Keith Thompson" <kst-u@mib.org> wrote in message 
news:lnbn9vgp9v.fsf@kst-u.example.com...
> "Bill Cunningham" <nospam@nspam.invalid> writes:
>>      I had an unsigned int a[512] similar say to sector size  of some
>> filesystems. My thinking was to use while and for anyway I ways thinking 
>> two
>> loops would be needed one inside the other perhaps. To fill the array 
>> a[512]
>> with the unsigned int value of 'F' which I believe is 70 in decimal. I 
>> can't
>> do it. Iterrations from 0 to 511 is going to be needed and each of the
>> elements a[0] for example will have to have 70 placed in there. I'm 
>> stuck.
>> So I tried this function. Will it do what I want? This code is tested and
>> each time I run it I get a different numeric value.
>>
>> #include <stdio.h>
>> #include <string.h>
>>
>> int main()
>> {
>>     unsigned int a[512];
>>     unsigned int *pu;
>>     pu = memset(a, 'F', 512);
>>     printf("%u\n", pu);
>> }
>>
>> It this in some way doing what I want? I want a hexdump to show 512 bytes 
>> of
>> F's. Or set bits. I know I could use the bitwise operators but, this 
>> might
>> be a better way.
>
> You say you know you could use the bitwise operators.  I don't see how.
>
> You say you want 512 *bytes* of 'F's, but your array is 512*sizeof(int)
> bytes, not 512 bytes.  If sizeof(unsigned int)==4, as is typical, then
> your array is 2048 bytes.  sizeof(unsigned int) can be larger or smaller
> than 4 on different systems.
>
> Yes, 'F' == 70 in ASCII.
>
> "int main()" is better written as "int main(void)".  (This is a minor
> point; "int main()" will also work.)
>
> Do you want each of the 512 unsigned int elements to hold the value 'F'
> or do you want each of the (probably) 2048 bytes of the array to hold
> the value 'F'?  Neither makes a whole lot of sense, so I can't guess
> which one you want.  If you want to store character values, you should
> probably use a character type, for example:
>    unsigned char a[512];
> (Or I suppose you could set just the first 512 bytes of your 2048-byte
> array to 'F', which is what your current code does, but that makes even
> less sense.)
>
> Let's assume sizeof (unsigned int) == 4, which is typical.  70 in
> hexadecimal is 0x46.  Do you want each element of your array to hold the
> value 0x00000046, or 0x46464646?  (This is a rephrasing of the same
> question I asked in the previous paragraph.)
>
> memset() returns the value of its first argument.  It has no way to
> indicate an error.  So there's no point in storing the value in another
> variable.  You can drop pu.
>
> Your printf call
>    printf("%u\n", pu);
> almost certainly doesn't do what you want.  It's an incorrect way of
> printing the *address* of (the beginning of) your array.  The correct
> way to print that address would be any of the following:
>    printf("%p\n", (void*)pu);
>    printf("%p\n", (void*)a);
>    printf("%p\n", (void*)&a);
>    printf("%p\n", (void*)&a[0]);
>
> To set each byte of the array to 'F' (thereby setting each element to
> 0x46464646):
>
>    memset(a, 'F', sizeof a);
>
> To set each element of the array to 'F' (0x00000046):
>
>    for (int i = 0; i < sizeof a / sizeof a[0]; i ++) {
>        a[i] = 'F';
>    }
>
> In that case you can drop the "#include <string.h>".  (The memset
> function sets a sequence of bytes.  There is no corresponding function
> to set a sequence of unsigned ints, so you have to write your own loop.)
>
> Printing the stored value of `a` is left as an exercise.

The 0x4646 would be ok. But here's where I'm getting the F.

0xFF  == 11111111

If a series of bytes or bit are set or "on" the hex value will be 0xFF or as 
in the olden days 0FFh.

Bill

[toc] | [prev] | [next] | [standalone]


#78513

From"Osmium" <r124c4u102@comcast.net>
Date2015-12-12 20:00 -0600
Message-ID<dd41pjF3cvmU1@mid.individual.net>
In reply to#78512
"Bill Cunningham" wrote:

>> You say you know you could use the bitwise operators.  I don't see how.

Bill has a rule to never confine a post to a single subject.  Those words 
were simply at the top of his push around list of words. 

[toc] | [prev] | [next] | [standalone]


#78514

From"Osmium" <r124c4u102@comcast.net>
Date2015-12-12 20:01 -0600
Message-ID<dd41sgF3de9U1@mid.individual.net>
In reply to#78513
"Osmium" <r124c4u102@comcast.net> wrote in message 
news:dd41pjF3cvmU1@mid.individual.net...
> "Bill Cunningham" wrote:
>
>>> You say you know you could use the bitwise operators.  I don't see how.
>
> Bill has a rule to never confine a post to a single subject.  Those words 
> were simply at the top of his push around list of words.

I don't know what happened on that attribution, it's clearly wrong. 

[toc] | [prev] | [next] | [standalone]


#78516

FromKeith Thompson <kst-u@mib.org>
Date2015-12-12 18:06 -0800
Message-ID<ln4mfngl0o.fsf@kst-u.example.com>
In reply to#78512
"Bill Cunningham" <nospam@nspam.invalid> writes:
> "Keith Thompson" <kst-u@mib.org> wrote in message 
> news:lnbn9vgp9v.fsf@kst-u.example.com...
>> "Bill Cunningham" <nospam@nspam.invalid> writes:
>>>      I had an unsigned int a[512] similar say to sector size of some
>>> filesystems. My thinking was to use while and for anyway I ways
>>> thinking two loops would be needed one inside the other perhaps. To
>>> fill the array a[512] with the unsigned int value of 'F' which I
>>> believe is 70 in decimal.
[...]
>>> #include <stdio.h>
>>> #include <string.h>
>>>
>>> int main()
>>> {
>>>     unsigned int a[512];
>>>     unsigned int *pu;
>>>     pu = memset(a, 'F', 512);
>>>     printf("%u\n", pu);
>>> }
[...]
>> To set each byte of the array to 'F' (thereby setting each element to
>> 0x46464646):
>>
>>    memset(a, 'F', sizeof a);
>>
>> To set each element of the array to 'F' (0x00000046):
>>
>>    for (int i = 0; i < sizeof a / sizeof a[0]; i ++) {
>>        a[i] = 'F';
>>    }
[...]
>
> The 0x4646 would be ok. But here's where I'm getting the F.

The two options I presented were 0x46 and 0x46464646.  Why are you
bringing up 0x4646?

> 0xFF  == 11111111
>
> If a series of bytes or bit are set or "on" the hex value will be 0xFF or as 
> in the olden days 0FFh.

Impressive.  You probably had more misconceptions than lines of
code in your example, and yet there was another that you managed
to hide away.

The letter F in the character constant 'F' and the letter F in the
hexadecimal constant 0xFF are unrelated.  In the latter, F is a digit
representing the value 15; 0xFF means exactly the same thing as 255.

If you really want help doing something, you need to explain what
the actual goal is.  Filling an array of unsigned int with the
character 'F', as I said, doesn't make much sense.  Filling it with
0xFF probably makes a little more sense, but there was no way anyone
could have figured out that when you wrote 'F' your really mean 0xFF.
I still have no clue why you want to do this, but you can do it
by replacing each occurrence of the character constant 'F' in my
suggested solutions above by the hexadecimal integer constant 0xFF.

If you're trying to initialize each byte to 0xFF, but "0x4646
would be ok", then I must conclude that you have no more of a clue
about what you're trying to accomplish than I have.  If setting
the array elements to 0xFF, 0xFFFFFFFF, or to 0x4646 would all be
acceptable, would turning off your computer and walking away be
equally acceptable?

What are you trying to do, and why?  Don't even think about writing
a line of code until you've decided that.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#78520

From"Bill Cunningham" <nospam@nspam.invalid>
Date2015-12-12 21:21 -0500
Message-ID<n4ikjf$tl5$1@dont-email.me>
In reply to#78516
"Keith Thompson" <kst-u@mib.org> wrote in message 
news:ln4mfngl0o.fsf@kst-u.example.com...

[snip]

> Impressive.  You probably had more misconceptions than lines of
> code in your example, and yet there was another that you managed
> to hide away.
>
> The letter F in the character constant 'F' and the letter F in the
> hexadecimal constant 0xFF are unrelated.  In the latter, F is a digit
> representing the value 15; 0xFF means exactly the same thing as 255.
>
> If you really want help doing something, you need to explain what
> the actual goal is.  Filling an array of unsigned int with the
> character 'F', as I said, doesn't make much sense.  Filling it with
> 0xFF probably makes a little more sense, but there was no way anyone
> could have figured out that when you wrote 'F' your really mean 0xFF.
> I still have no clue why you want to do this, but you can do it
> by replacing each occurrence of the character constant 'F' in my
> suggested solutions above by the hexadecimal integer constant 0xFF.
>
> If you're trying to initialize each byte to 0xFF, but "0x4646
> would be ok", then I must conclude that you have no more of a clue
> about what you're trying to accomplish than I have.

Ok If I used this command on a unix console,

hexdump -n 512 file.com

0...
0200...

Was all 0xFF I would sing like a bird. I have no idea how to accomplish it 
and it might not be practical. But it's an experiment. Maybe more than I can 
chew. A series likewise only 0x4646 would be ok too. An accomplished 
pattern.

  If setting
> the array elements to 0xFF, 0xFFFFFFFF, or to 0x4646 would all be
> acceptable, would turning off your computer and walking away be
> equally acceptable?
>
> What are you trying to do, and why?  Don't even think about writing
> a line of code until you've decided that.

[toc] | [prev] | [next] | [standalone]


#78523

FromKeith Thompson <kst-u@mib.org>
Date2015-12-12 18:41 -0800
Message-ID<lnio43f4t3.fsf@kst-u.example.com>
In reply to#78520
"Bill Cunningham" <nospam@nspam.invalid> writes:
> "Keith Thompson" <kst-u@mib.org> wrote in message 
> news:ln4mfngl0o.fsf@kst-u.example.com...
[...]
>> If you're trying to initialize each byte to 0xFF, but "0x4646
>> would be ok", then I must conclude that you have no more of a clue
>> about what you're trying to accomplish than I have.
>
> Ok If I used this command on a unix console,
>
> hexdump -n 512 file.com
>
> 0...
> 0200...
> 
> Was all 0xFF I would sing like a bird. I have no idea how to accomplish it 
> and it might not be practical. But it's an experiment. Maybe more than I can 
> chew. A series likewise only 0x4646 would be ok too. An accomplished 
> pattern.

You've asked how to initialize an array.  For some unknown reason,
you've defined it as an array of unsigned int (why?).

How do you expect the contents of the array to get into your file?

(I'd ask why you decided to name the file with a ".com" extension, but
it doesn't matter.)

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#78518

From"Bill Cunningham" <nospam@nspam.invalid>
Date2015-12-12 21:12 -0500
Message-ID<n4ik1u$sbv$1@dont-email.me>
In reply to#78506
"Keith Thompson" <kst-u@mib.org> wrote in message 
news:lnbn9vgp9v.fsf@kst-u.example.com...

> You say you know you could use the bitwise operators.  I don't see how.

    If I could have a series of bits unset I could XOR  and have them set. I 
must not know what I'm talking about.

> You say you want 512 *bytes* of 'F's, but your array is 512*sizeof(int)
> bytes, not 512 bytes.  If sizeof(unsigned int)==4, as is typical, then
> your array is 2048 bytes.  sizeof(unsigned int) can be larger or smaller
> than 4 on different systems.
>
> Yes, 'F' == 70 in ASCII.
>
> "int main()" is better written as "int main(void)".  (This is a minor
> point; "int main()" will also work.)
>
> Do you want each of the 512 unsigned int elements to hold the value 'F'
> or do you want each of the (probably) 2048 bytes of the array to hold
> the value 'F'?  Neither makes a whole lot of sense, so I can't guess
> which one you want.  If you want to store character values, you should
> probably use a character type, for example:
>    unsigned char a[512];
> (Or I suppose you could set just the first 512 bytes of your 2048-byte
> array to 'F', which is what your current code does, but that makes even
> less sense.)
>
> Let's assume sizeof (unsigned int) == 4, which is typical.  70 in
> hexadecimal is 0x46.  Do you want each element of your array to hold the
> value 0x00000046, or 0x46464646?  (This is a rephrasing of the same
> question I asked in the previous paragraph.)
>
> memset() returns the value of its first argument.  It has no way to
> indicate an error.  So there's no point in storing the value in another
> variable.  You can drop pu.
>
> Your printf call
>    printf("%u\n", pu);
> almost certainly doesn't do what you want.  It's an incorrect way of
> printing the *address* of (the beginning of) your array.  The correct
> way to print that address would be any of the following:
>    printf("%p\n", (void*)pu);
>    printf("%p\n", (void*)a);
>    printf("%p\n", (void*)&a);
>    printf("%p\n", (void*)&a[0]);
>
> To set each byte of the array to 'F' (thereby setting each element to
> 0x46464646):
>
>    memset(a, 'F', sizeof a);
>
> To set each element of the array to 'F' (0x00000046):
>
>    for (int i = 0; i < sizeof a / sizeof a[0]; i ++) {
>        a[i] = 'F';
>    }
>
> In that case you can drop the "#include <string.h>".  (The memset
> function sets a sequence of bytes.  There is no corresponding function
> to set a sequence of unsigned ints, so you have to write your own loop.)
>
> Printing the stored value of `a` is left as an exercise.

    I think maybe I should stick to tutorials on algorithms and how to do 
them in C. I have been looking at this a bit.

struct node {
    int key;
    struct node *left;
    struct node *right;
};

struct node *root=0;

If I stick with these things there might be less confusion. Tutorial not or 
only algorithms but algorithms in C.

Bill

[toc] | [prev] | [next] | [standalone]


#78521

FromKeith Thompson <kst-u@mib.org>
Date2015-12-12 18:28 -0800
Message-ID<lnr3irf5f5.fsf@kst-u.example.com>
In reply to#78518
"Bill Cunningham" <nospam@nspam.invalid> writes:
> "Keith Thompson" <kst-u@mib.org> wrote in message 
> news:lnbn9vgp9v.fsf@kst-u.example.com...
>
>> You say you know you could use the bitwise operators.  I don't see how.
>
>     If I could have a series of bits unset I could XOR  and have them set. I 
> must not know what I'm talking about.

You could XOR them *with what?

If an unsigned int is set to all-bits-zero, you can set it to whatever
value you like simply by assigning that value.  There's no need to use
XOR.

[...]

>     I think maybe I should stick to tutorials on algorithms and how to do 
> them in C. I have been looking at this a bit.
>
> struct node {
>     int key;
>     struct node *left;
>     struct node *right;
> };
>
> struct node *root=0;
>
> If I stick with these things there might be less confusion. Tutorial not or 
> only algorithms but algorithms in C.

Yeah, that's a binary tree.  It has nothing at all to do with what you
were talking about a moment ago.  Rhinoceros.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#78509

FromIan Collins <ian-news@hotmail.com>
Date2015-12-13 13:56 +1300
Message-ID<dd3u2nFh4leU3@mid.individual.net>
In reply to#78499
Bill Cunningham wrote:
>       I had an unsigned int a[512] similar say to sector size  of some
> filesystems. My thinking was to use while and for anyway I ways thinking two
> loops would be needed one inside the other perhaps. To fill the array a[512]
> with the unsigned int value of 'F' which I believe is 70 in decimal. I can't
> do it. Iterrations from 0 to 511 is going to be needed and each of the
> elements a[0] for example will have to have 70 placed in there. I'm stuck.
> So I tried this function. Will it do what I want? This code is tested and
> each time I run it I get a different numeric value.
>
> #include <stdio.h>
> #include <string.h>
>
> int main()
> {
>      unsigned int a[512];
>      unsigned int *pu;
>      pu = memset(a, 'F', 512);
>      printf("%u\n", pu);
> }
>
> It this in some way doing what I want? I want a hexdump to show 512 bytes of
> F's. Or set bits. I know I could use the bitwise operators but, this might
> be a better way.

5/10.  Your standards are slipping.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#78542

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2015-12-13 15:42 +0000
Message-ID<slrnn6r4g0.5q5.grahn+nntp@frailea.sa.invalid>
In reply to#78499
On Sat, 2015-12-12, Bill Cunningham wrote:
>      I had an unsigned int a[512] similar say to sector size  of some 
> filesystems. My thinking was to use while and for anyway I ways thinking two 
> loops would be needed one inside the other perhaps. To fill the array a[512] 
> with the unsigned int value of 'F' which I believe is 70 in decimal. I can't 
> do it. Iterrations from 0 to 511 is going to be needed and each of the 
> elements a[0] for example will have to have 70 placed in there. I'm stuck. 

You more or less stated it, except in words:

   unsigned a[512];
   for (unsigned i=0; i<512; i++) a[i] = 'F';

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [next] | [standalone]


#78543

From"Bill Cunningham" <nospam@nspam.invalid>
Date2015-12-13 11:09 -0500
Message-ID<n4k52s$dor$1@dont-email.me>
In reply to#78542
"Jorgen Grahn" <grahn+nntp@snipabacken.se> wrote in message 
news:slrnn6r4g0.5q5.grahn+nntp@frailea.sa.invalid...

> You more or less stated it, except in words:
>
>   unsigned a[512];
>   for (unsigned i=0; i<512; i++) a[i] = 'F';

    Oh I see. In some things before posting I did have a bit of code with 
f=a[i]. I guess that was backward. That would be loading from the array into 
F. That's wrong. Thanks for this code. It triggers memories.

    The thing is the Dr. told me. "In 5 years you won't be able to find your 
way home". It's the medicine I'm taking. If I can get it cut some I might be 
better. And there's more to life than C. I don't want to bother you with my 
troubles though. K and R 2 always confused me for some reason. But I have 
several C books and my law books. :)

Bill

[toc] | [prev] | [next] | [standalone]


#78567

From"Chris M. Thomasson" <nospam@nospam.nospam>
Date2015-12-13 12:35 -0800
Message-ID<n4kkrs$8o0$1@speranza.aioe.org>
In reply to#78543
> "Bill Cunningham"  wrote in message news:n4k52s$dor$1@dont-email.me...
[...]

>     The thing is the Dr. told me. "In 5 years you won't be able to find 
> your way home". It's the medicine I'm taking. If I can get it cut some I 
> might be better.

medicine! Are you going to some dr.feelgood quack bastard?

WTF is that piece of shi% drugging you with? 

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c


csiph-web