I was looking at the Sendmail's source code, and i've got confused
about this kind of initialization:
------------------------
struct prival PrivacyValues[] =
{
{ "public", PRIV_PUBLIC },
{ "needmailhe lo", PRIV_NEEDMAILHE LO },
{ "needexpnhe lo", PRIV_NEEDEXPNHE LO },
{ "needvrfyhe lo", PRIV_NEEDVRFYHE LO },
....
};
------------------------
I'm familiar with char string[] = "foo", but i have no idea what the
above snip of code is doing. Anyone could please explain? 14 2414
gustavo wrote: I was looking at the Sendmail's source code, and i've got confused about this kind of initialization:
------------------------ struct prival PrivacyValues[] = { { "public", PRIV_PUBLIC }, { "needmailhe lo", PRIV_NEEDMAILHE LO }, { "needexpnhe lo", PRIV_NEEDEXPNHE LO }, { "needvrfyhe lo", PRIV_NEEDVRFYHE LO }, ... }; ------------------------
I'm familiar with char string[] = "foo", but i have no idea what the above snip of code is doing. Anyone could please explain?
It is just the initialization of an array of structure.
It can be done in another way in your code.
Eg:
PrivayValues[0].field1 = "public";
PrivayValues[0].field2 = PRIV_PUBLIC;
PrivayValues[1].field1 = "needmailhe lo";
PrivayValues[1].field2 = PRIV_NEEDMAILHE LO;
-----
PrivayValues[n].field1 = "--";
PrivayValues[n].field2 = --;
can you also provide what is struct prival..
I suppose it must be
struct prival
{
char *x;
int y;
};
In the above case,
one element of the struct can be created using
struct prival OneElement = { "anystring" , ANY_INTEGER };
The first member might be confusing. What it does is
1. Stores "anystring" into the string table.
2. Puts a pointer in OneElement.x
In a C program whereever string constants occur, they are replaced by
pointers and the string constants are moved to the string table.
But, array initialization is an exception to that.
when we write string[] = "foo",
the compiler actually takes it as
string[4] = { 'f', 'o', 'o' , '\0' };
and therefore "foo" does not go to the string table at all.
Regards,
Yada Kishore
gustavo wrote: I was looking at the Sendmail's source code, and i've got confused about this kind of initialization:
------------------------ struct prival PrivacyValues[] = { { "public", PRIV_PUBLIC }, { "needmailhe lo", PRIV_NEEDMAILHE LO }, { "needexpnhe lo", PRIV_NEEDEXPNHE LO }, { "needvrfyhe lo", PRIV_NEEDVRFYHE LO }, ... }; ------------------------
I'm familiar with char string[] = "foo", but i have no idea what the above snip of code is doing. Anyone could please explain?
char string[] = "foo";
is actually just a convenient shortcut of the more general form:
char string[] = {
'f' ,
'o' ,
'o' ,
'\0'
};
The general form applies to all types in C, for example:
int example[] = {
100,
200,
300
};
this includes structs, for example:
struct thing {
char *name;
int size;
};
struct thing stuff[] = {
{"Me", 100},
{"Myself", 200},
{"I", 300}
};
gustavo wrote: I was looking at the Sendmail's source code, and i've got confused about this kind of initialization:
------------------------ struct prival PrivacyValues[] = { { "public", PRIV_PUBLIC }, { "needmailhe lo", PRIV_NEEDMAILHE LO }, { "needexpnhe lo", PRIV_NEEDEXPNHE LO }, { "needvrfyhe lo", PRIV_NEEDVRFYHE LO }, ... }; ------------------------
I'm familiar with char string[] = "foo", but i have no idea what the above snip of code is doing. Anyone could please explain?
[The quote above is to identify the thread]
After reading the responses to this thread, I started wondering whether
the 'universal initialiser' {0} could apply to types like int which can
be initialised normally. So I wrote the program
#include <stdio.h>
int i = {0};
int j = {1};
int main(void)
{
printf("%d %d\n",i,j);
return 0;
}
which compiles on c89-conforming gcc with warnings turned up to the max
with no diagnostics, printing
0 1
as expected. Is it really possible to initialise anything like this?
ais523 wrote:
<snip> After reading the responses to this thread, I started wondering whether the 'universal initialiser' {0} could apply to types like int which can be initialised normally. So I wrote the program
#include <stdio.h>
int i = {0}; int j = {1};
int main(void) { printf("%d %d\n",i,j); return 0; }
which compiles on c89-conforming gcc with warnings turned up to the max with no diagnostics, printing 0 1 as expected. Is it really possible to initialise anything like this?
Yes, this is perfectly legal C and is defined as doing what you expect.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro: http://clc-wiki.net/wiki/Intro_to_clc
Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Flash Gordon wrote: ais523 wrote:
<snip>
After reading the responses to this thread, I started wondering whether the 'universal initialiser' {0} could apply to types like int which can be initialised normally. So I wrote the program
#include <stdio.h>
int i = {0}; int j = {1};
int main(void) { printf("%d %d\n",i,j); return 0; }
which compiles on c89-conforming gcc with warnings turned up to the max with no diagnostics, printing 0 1 as expected. Is it really possible to initialise anything like this?
Yes, this is perfectly legal C and is defined as doing what you expect.
struct test_t {
char *a;
int b;
double c;
};
int main(void)
{
struct test_t test = { 0 };
}
Will this initialization set the whole struct to 0,
or just the first member, byte, or something like that?
ciju wrote: gustavo wrote: I was looking at the Sendmail's source code, and i've got confused about this kind of initialization:
------------------------ struct prival PrivacyValues[] = { { "public", PRIV_PUBLIC }, { "needmailhe lo", PRIV_NEEDMAILHE LO }, { "needexpnhe lo", PRIV_NEEDEXPNHE LO }, { "needvrfyhe lo", PRIV_NEEDVRFYHE LO }, ... }; ------------------------
I'm familiar with char string[] = "foo", but i have no idea what the above snip of code is doing. Anyone could please explain?
It is just the initialization of an array of structure. It can be done in another way in your code.
Eg: PrivayValues[0].field1 = "public"; PrivayValues[0].field2 = PRIV_PUBLIC;
This is not the same thing at all. You have assignment, not
initialization. If field1 happens to be an array of char rather than a
pointer to char, your version would be illegal.
Similarly, if either field1 or field2 happened to be const.
Brian
edware wrote:
<snip> struct test_t { char *a; int b; double c; };
int main(void) { struct test_t test = { 0 }; }
Will this initialization set the whole struct to 0, or just the first member, byte, or something like that?
It will initialise the entire struct so appropriate 0 type values (null
pointers, 0, 0.0 as appropriate). If you initialise any of it to any
value then the rest gets initialised to an appropriate 0 value for the type.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro: http://clc-wiki.net/wiki/Intro_to_clc
Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Flash Gordon wrote: edware wrote:
<snip>
struct test_t { char *a; int b; double c; };
int main(void) { struct test_t test = { 0 }; }
Will this initialization set the whole struct to 0, or just the first member, byte, or something like that?
It will initialise the entire struct so appropriate 0 type values (null pointers, 0, 0.0 as appropriate). If you initialise any of it to any value then the rest gets initialised to an appropriate 0 value for the type.
NOT to null pointers. Those you have to handle yourself.
--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jacek Dziedzic |
last post by:
Hi!
A) Why isn't it possible to set a member of the BASE class in
an initialization list of a DERIVED class constructor (except for
'calling' the base constructor from there, of course)? I even
tried prefixing them with BASE:: but to no avail. Still it's
ok when I set them in the construtor body, not the init list.
Does this mean that it's a small advantage of the initialization
inside the constructor over initialization in the init...
|
by: Petter Reinholdtsen |
last post by:
Is the code fragment 'char a = ("a");' valid ANSI C? The
problematic part is '("a")'. I am sure 'char a = "a";' is valid
ANSI C, but I am more unsure if it is allowed to place () around the
string literal.
|
by: Kurt Ng |
last post by:
Need help quick!!! Am really stuck on this problem!
I have a C dll, and it uses a nested struct. (see below) The struct has 2
layers of nested structs. The first field in the first struct is a char*.
I used StructLayout.Sequential to port each of the structs to C#, and I used
UnmanagedType.LPStr to marshal the char* to C# string, but the resulting
struct data are stored wrong when passed into the dll's api call. The char*
is...
|
by: Jordan Tiona |
last post by:
How can I make one of these? I'm trying to get my program to store a string
into a variable, but it only stores one line.
--
"No eye has seen, no ear has heard, no mind can
conceive what God has prepared for those who love him"
1 Cor 2:9
|
by: fei.liu |
last post by:
Consider the following sample code
char * ptr = "hello";
char carray = "hello";
int main(void){
}
What does the standard have to say about the storage requirement
about ptr and carray? Is it a fair statement that char *ptr will take
4 more bytes (on 32bit platform) in DATA segment? I have found
| |
by: wkaras |
last post by:
I've compiled this code:
const int x0 = 10;
const int x1 = 20;
const int x2 = 30;
int x = { x2, x0, x1 };
struct Y
{
|
by: thinktwice |
last post by:
char a = { 0 }
is it ok?
|
by: mdh |
last post by:
And I thought I understood it, finally. Alas.
given:
char *s={"Jan","Feb","Mar","April"};
is it possible to have char *p point at s?
*p = s...does not do it, as I expect.
*p=s...I believe sets it to point to the first element in s. But
|
by: DomoChan |
last post by:
the code below will compile in visual c++ 2003, but im not sure its
valid.
unsigned char myString = "";
after this line executes, all the bytes within myString are indeed set
to '0's' but is this really valid c++ or c? where can I find out how
this is implemented?
Im concerned because I had a 3rd party library wrapper which was
|
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: 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: 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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |