linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Charlie Gordon" <gmane@chqrlie.org>
To: linux-c-programming@vger.kernel.org
Subject: Re: Question on memcpy()
Date: Wed, 15 Sep 2004 16:34:37 +0200	[thread overview]
Message-ID: <ci9jsn$1jq$1@sea.gmane.org> (raw)
In-Reply-To: E3E30069B061524E90BCEE4417E3066113852B@monm207.monmouth.army.mil

"Huber, George K RDECOM CERDEC STCD SRI" <George.K.Huber@us.army.mil> wrote
in message
news:E3E30069B061524E90BCEE4417E3066113852B@monm207.monmouth.army.mil...
> Wen wrote,
>
> >struct sample A, B;
> >...
> >...
> >memset(&A, 0, sizeof(sample));
> >memcpy(&A, (char *)&B, sizeof(sample));
> >...
> >I could not understand why the second argument is (char *)&B, but not &B.
> >I think it should be &B.
>

This is correct, I suspect this cast comes from the programmer ancient
belief that memcpy should be passed char pointers (confusingly refered to as
byte pointers).  Since it is defined to take (void *, const void*, size_t) ,
passing it any type of pointer is fine, including superfluous casts.

It should be noted that while the initialization to 0 required a call to
memset, copying the raw contents of une struct to another can be done more
efficiently (usually) in C by writing:

A = B;

as far as why sizeof(sample) is accepted at all by the compiler, there 3
possible explanations:

- there is a typedef for sample, at it better be defined to struct sample.
- there is a variable by the name sample in scope, and it better be an
instance of struct sample.
- there is a #define defining the identifier sample to something valid as an
operand to sizeof.  Again, it better be consistent with the use in these 2
lines.

It would be better to make no assumptions about the types of A and B and
write:

memset(&A, 0, sizeof(A));
...
A = B;

This way, the compiler will protest if A and B types don't match, and will
compute the right size for A no matter what type it's defined to be.

Chqrlie.




  reply	other threads:[~2004-09-15 14:34 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-15 14:14 Question on memcpy() Huber, George K RDECOM CERDEC STCD SRI
2004-09-15 14:34 ` Charlie Gordon [this message]
  -- strict thread matches above, loose matches on Subject: below --
2004-09-15  8:33 RedHat Itanium 64 GCC 3.2.3 linking problems sheetal fegde
2004-09-15 11:17 ` Question on memcpy() Wen Guangcheng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='ci9jsn$1jq$1@sea.gmane.org' \
    --to=gmane@chqrlie.org \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).