S
sandSpiderX
Hi,
What is meant by
where X is
struct X
{
int i;
}
const X cx={1};
Help
sandspiderX
What is meant by
where X is
struct X
{
int i;
}
const X cx={1};
Help
sandspiderX
sandSpiderX said:Hi,
What is meant by
where X is
struct X
{
int i;
}
const X cx={1};
sandSpiderX said:Hi,
What is meant by
where X is
struct X
{
int i;
}
const X cx={1};
Help
sandspiderX
benben said:It's initializing cx with cx.i assigned to 1. Same as X cx; cx.i = 1;
more example:
struct font
{
int size;
char* family;
int color_red;
int color_green;
int color_blue;
bool bold;
bool italic;
};
// you can write
font myfont = {12, "times new roman", 255, 10, 10, true, false};
// instead of
font myfont2;
myfont2.size = 12;
myfont2.family = "times new roman";
myfont2.color_red = 255;
myfont2.color_green = 10;
myfont2.color_blue = 10;
myfont2.bold = true;
myfont2.italic = false;
regards,
ben
sandSpiderX said:Hi,
What is meant by
where X is
struct X
{
int i;
}
const X cx={1};
Help
sandspiderX
sandSpiderX said:Hi,
Can I do this for class also
Like
class X
{
public:
int x;
};
const class X = {1};
Help
ss
It's initializing cx with cx.i assigned to 1. Same as X cx; cx.i = 1;
more example:
struct font
{
int size;
char* family;
int color_red;
int color_green;
int color_blue;
bool bold;
bool italic;
};
[...]
myfont2.family = "times new roman";
sandSpiderX said:Hi,
What is meant by
where X is
struct X
{
int i;
}
const X cx={1};
Help
sandspiderX
: > myfont2.family = "times new roman";
:
: imho, you'll get a problem with just storing a pointer to a character
: sequence, which will be gone as soon as the above line has been
executed.
Not at all: string literals have static storage.
In other words, the above line is equivalent to:
static char const anonymous_string_literal[] = "times new roman";
myfont2.family = anonymous_string_literal;
The storage of string literals persists until program exit.
: your struct should contain char family[MAX_NUMBER_OF_CHARS + 1]
instead of
: char* family!
No, this would expose you to a whole range of serious
bugs (i.e. buffer overruns). If a 'naked' char const*
is inadequate, better use std::string .
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.