I mean:
unsigned char foo[bar] = "";
is the same as:
unsigned char foo[bar];
memset (foo, '\0', bar);
?
Thanks! 7 2082
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
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)
iD4DBQFCzXLRbt3 SB/zFBA8RAjFZAKDXi c+4JVuHaB0hWt29 NlMfkUnwGACWJzs 4
0pf+WHz1IZy5vSV hUB5ZEQ==
=xxXJ
-----END PGP SIGNATURE-----
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.
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
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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"
|
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 foo {
};
|
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 something once the
subject matters gets stickier? MPJ
#include <stdio.h>
int main(void){
int i = 5;
void increment(int *);
increment(&i);
printf("i = %d\n",i);
|
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 'mem'. Can one do:
offsetof(struct foo, bar.mem)
Thanks,
Mike
|
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?
When I try to open the file from .aspx the file read throws: Logon failure:
Logon failure: unknown user name or bad password.
When I try to open the file from a custom IHttpHandler the file read throws:
Logon failure: unknown user name or bad...
| |
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 a
function pointer, but i sorta want to go the other way around.
thanks
class Foo { public: void bar() {} };
|
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 project where you want some encapsulation.
But a lot of it being grouping of what would otherwise be static members
of the global namespace. So that instead of
|
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 of type Foo.Bar.
Foo.Bar data = (For.Bar)input;
Thanks,
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |