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


Groups > comp.lang.c > #78506

Re: Ok how do I do this

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Ok how do I do this
Date 2015-12-12 16:34 -0800
Organization None to speak of
Message-ID <lnbn9vgp9v.fsf@kst-u.example.com> (permalink)
References <n4i624$det$1@dont-email.me>

Show all headers | View raw


"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"

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web