473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Are foo[bar] = "" (at declaration) and memset (foo, '\0', bar) the same?

I mean:

unsigned char foo[bar] = "";

is the same as:

unsigned char foo[bar];
memset (foo, '\0', bar);

?

Thanks!

Nov 15 '05 #1
7 2067
adelfino wrote:
I mean:

unsigned char foo[bar] = "";

is the same as:

unsigned char foo[bar];
memset (foo, '\0', bar);


I believe the former only sets the first character to \0 while the
second will set all of them.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 15 '05 #2
Hi,
?
No. They're not the same.
unsigned char foo[bar] = "";
According to ANSI C90 this shouldn't work at all.

unsigned char foo[bar];
generates an array of unsigned char with size bar on the stack.

unsigned char foo = "";
generates an (const) array of unsigned char consisting of '\0' and
nothing else.
unsigned char foo[bar];
memset (foo, '\0', bar);


memset() sets _all_ bytes of foo to '\0'.

best regards,
Stephan

--
Stephan Beyer, PGP 0xFCC5040F, IRC sbeyer (seebyr, bseyer)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD4DBQFCzXLRbt3SB/zFBA8RAjFZAKDXic+4JVuHaB0hWt29NlMfkUnwGACWJzs4
0pf+WHz1IZy5vSVhUB5ZEQ==
=xxXJ
-----END PGP SIGNATURE-----

Nov 15 '05 #3
Me
adelfino wrote:
I mean:

unsigned char foo[bar] = "";

is the same as:

unsigned char foo[bar];
memset (foo, '\0', bar);

?


For this specific example, yes. You can also do:

unsigned char foo[bar] = { 0 };
unsigned char foo[bar] = { '\0' };

However, changing this to a signed char (or plain char if the
underlying type is signed char) is not guaranteed to be the same thing
as there may be padding bits. They have the same values, but memcmp may
not return 0 because it's unspecified what these padding bits contain.

Nov 15 '05 #4
Jonathan Bartlett wrote:
adelfino wrote:
I mean:

unsigned char foo[bar] = "";

is the same as:

unsigned char foo[bar];
memset (foo, '\0', bar);


I believe the former only sets the first character to \0 while the
second will set all of them.


You're entitled to believe whatever you like (have you
heard about the faked Moon landings?), but you might want to
put your faith to the test by reading Section 6.7.8 paragraph 21:

"If there are [...] fewer characters in a string
literal used to initialize an array of known size
than there are elements in the array, the remainder
of the aggregate shall be initialized implicitly the
same as objects that have static storage duration."

.... and paragraph 10:

"[...] If an object that has static storage duration
is not initialized explicitly, then [...] if it has
arithmetic type, it is initialized to [...] zero [...]"

--
Eric Sosman
es*****@acm-dot-org.invalid

Nov 15 '05 #5
Stephan Beyer wrote:
Hi,

?

No. They're not the same.

unsigned char foo[bar] = "";

According to ANSI C90 this shouldn't work at all.


It depends on what `bar' is. If it's a #define'd
constant integer expression or a positive-valued enum
constant, this is just fine for C90. C99 expands the
universe of acceptable `bar' to include positive-valued
run-time expressions.
unsigned char foo[bar];
generates an array of unsigned char with size bar on the stack.
Assuming `bar' is something acceptable, this defines
such an array. Where that array resides is not known: it
might have static storage duration or automatic storage
duration depending on the context, and in either case it's
the implementation's business to figure out where to store it.
unsigned char foo = "";
generates an (const) array of unsigned char consisting of '\0' and
nothing else.


No, it generates a required diagnostic under both C90
and C99 rules. Pointer-to-`char' and `char' are not compatible
types.
unsigned char foo[bar];
memset (foo, '\0', bar);


memset() sets _all_ bytes of foo to '\0'.


Yes, assuming an acceptable `bar'.

--
Eric Sosman
es*****@acm-dot-org.invalid

Nov 15 '05 #6
Jonathan Bartlett wrote:
adelfino wrote:
I mean:

unsigned char foo[bar] = "";

is the same as:

unsigned char foo[bar];
memset (foo, '\0', bar);


I believe the former only sets the first character to \0 while the
second will set all of them.


You believe wrong. The standard explicitly states that the rest of the
array will be zeroed.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #7
Me wrote:
adelfino wrote:
I mean:

unsigned char foo[bar] = "";

is the same as:

unsigned char foo[bar];
memset (foo, '\0', bar);

?


For this specific example, yes. You can also do:

unsigned char foo[bar] = { 0 };
unsigned char foo[bar] = { '\0' };

However, changing this to a signed char (or plain char if the
underlying type is signed char) is not guaranteed to be the same
thing as there may be padding bits.


It is guaranteed...

6.2.5p9

The range of nonnegative values of a signed integer type is a
subrange of the corresponding unsigned integer type, and the
representation of the same value in each type is the same...

Since unsigned char is unpadded, the representation of 0 in plain
or signed char is also all bits zero. [Because that's its
representation as an unsigned char.]

--
Peter

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Daniel | last post by:
how to make sure a xsl document has valid xsl syntax? i tried loading it into an xml document but that doesnt show syntax errors inside attributes such as "foo/bar" vs "bar\foo"
5
by: vilhelm.sjoberg | last post by:
Hello, I am a resonably confident C programmer, but not very sure about the dark corners of C++. Recently, I had G++ give me a strange error. The program in question is in essence: struct...
10
by: Merrill & Michele | last post by:
The following progs differ only in the location of the prototype (<--adjective) definition with respect to the main call. Both build and behave for me. Is there a difference that amounts to...
3
by: Michael B Allen | last post by:
Can offsetof be used to determine the offset of a member within an embedded struct member? For example, let 'struct foo' be a structure with an embedded structure 'struct bar' which has a member...
1
by: Daniel | last post by:
How to access a network file path \\foo\bar.txt from an .aspx and from a custom IHttpHandler? How to specify which account that aspx application should use when doing file IO from network paths?...
2
by: Ray Tayek | last post by:
hi, trying to make an array of function pointers to make delegates with. but the compiler does not like: void (Foo::^p)()=&Foo::bar; i did find an article that showed how to convert a delegate to...
14
by: António Marques | last post by:
Hi! I don't think I've ever been here, so I assume I'm addressing a bunch of nice people. Can you help me? I don't think there's a solution, but who knows. The thing is, picture a large...
10
by: mypetrock | last post by:
Has anyone run into this error message? Unable to cast object of type 'Foo.Bar' to type 'Foo.Bar'. I'm trying to cast an object of type Foo.Bar that I got out of a hash table into a variable...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.