Talk About Network

Google





Programming > C - C++ Learning > Re: [C] Pointer...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 24 Topic 4140 of 4400
Post > Topic >>

Re: [C] Pointer to an array

by Ben Bacarisse <ben.usenet@[EMAIL PROTECTED] > May 14, 2008 at 05:44 PM

Aggro <spammerdream@[EMAIL PROTECTED]
> writes:

> --------------- output from my console ------------
> $ cat code1.c
> #include <string.h>
> int main()
> {
>   char usr_data[128];
>   memset( &usr_data, 0, 128 );
>   return 0;
> }
>
> $ gcc code1.c
> $ md5sum a.out
> 653c4838fdbbdde05c832f79765fa143  a.out
> $ nano code1.c
> $ cat code1.c
> #include <string.h>
> int main()
> {
>   char usr_data[128];
>   memset( usr_data, 0, 128 );
>   return 0;
> }
>
> $ gcc code1.c
> $ md5sum a.out
> 653c4838fdbbdde05c832f79765fa143  a.out
> --------------- output from my console ------------
>
> There a few things I would like you to note.
> 1. The parameter for the memset is first "&usr_data", then it is
> "usr_data"

OK.

> 2. The md5sum for the generated binary file is identical. Also the
> behaviour as far as I could tell is identical. Also sha1sum is
> identical. So it looks like the binary files are identical.

I am sure they are.  You could have kept both and compared them using
cmp, but the check-sum method is simple.  In fact, in both cases the
compiler could optimise the memset away.  For this sort of thing, I'd
pass the array on to an another function as yet unwritten and compile
with -c, but even then, your question would still stand.

> Question: How is it possible that the binary files are identical with
> these two different source codes?

Because they that have sufficiently similar meanings that the compiler
generates the same code.

> And how is it possible that both applications fill the usr_data with
> zeroes? Shouldn't the first example just cause somekind of error? I
> would understand if the behaviour in that case would be undefined and
> is just happens to work correctly, but because the binary files are
> identical, it doesn't make sence.

In the first case, you pass a pointer to an array of 128 char.  It is
fine to convert this pointer to a void * as memset requires and no
error message is required.  This pointer to the array is in all
likelihood identical (in value) to the one you get in the second case.
It has a different type, but you don't do anything with that can
reveal this difference.

The second case is the "normal" one so I assume you know why that one
is fine!  (The name of the array -- an array-valued expression -- is
converted to a pointer to its first element and then that is converted
to a void * for memset.)

Does that help?

-- 
Ben.
 




 24 Posts in Topic:
[C] Pointer to an array
Aggro <spammerdream@[E  2008-05-14 15:14:02 
Re: [C] Pointer to an array
Richard Heathfield <rj  2008-05-14 16:30:48 
Re: [C] Pointer to an array
Ben Bacarisse <ben.use  2008-05-14 17:44:04 
Re: [C] Pointer to an array
Aggro <spammerdream@[E  2008-05-14 18:15:40 
Re: [C] Pointer to an array
Richard Heathfield <rj  2008-05-14 18:32:05 
Re: [C] Pointer to an array
Aggro <spammerdream@[E  2008-05-14 18:36:49 
Re: [C] Pointer to an array
Richard Heathfield <rj  2008-05-14 18:55:56 
Re: [C] Pointer to an array
Keith Thompson <kst-u@  2008-05-14 13:10:50 
Re: [C] Pointer to an array
Andrey Tarasevich <and  2008-05-14 11:41:42 
Re: [C] Pointer to an array
Anand Hariharan <znvyg  2008-05-21 04:44:12 
Re: [C] Pointer to an array
"Alf P. Steinbach&qu  2008-05-21 06:38:33 
Re: [C] Pointer to an array
Richard Heathfield <rj  2008-05-21 06:14:08 
Re: [C] Pointer to an array
Barry Schwarz <schwarz  2008-05-25 10:44:26 
Re: Pointer to an array
Anand Hariharan <mailt  2008-05-21 07:50:59 
Re: Pointer to an array
"Alf P. Steinbach&qu  2008-05-21 17:11:16 
Re: Pointer to an array
Bart van Ingen Schenau &l  2008-05-21 19:39:27 
Re: Pointer to an array
Andrey Tarasevich <and  2008-05-21 11:50:21 
Re: Pointer to an array
Anand Hariharan <mailt  2008-05-22 13:51:12 
Re: [C] Pointer to an array
Ben Bacarisse <ben.use  2008-05-26 16:57:06 
Re: [C] Pointer to an array
Francis Glassborow <fr  2008-05-26 19:39:07 
Re: [C] Pointer to an array
Keith Thompson <kst-u@  2008-05-26 14:14:19 
Re: [C] Pointer to an array
Ben Bacarisse <ben.use  2008-05-27 04:01:45 
Re: [C] Pointer to an array
Ben Bacarisse <ben.use  2008-05-27 04:23:15 
Re: [C] Pointer to an array
Andrey Tarasevich <and  2008-05-14 11:32:35 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Fri Jan 9 6:16:34 PST 2009.