Sign in to follow this  

c++ initiliazation what do you call it

This topic is 1984 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When you make a call like

UINT32 a = null, b;

What do you call that? I have noticed it in some code and I have never seen an initiliazation like that before. Visual C++ btw if it matters.

Share this post


Link to post
Share on other sites
What's your question?

You just define two variables [i]a[/i] and [i]b[/i], both being type [i]UINT32[/i] and [i]a[/i] is initialized to [i]null[/i].

Share this post


Link to post
Share on other sites
Hmm well I have never seen it done like that. Is there a name for it?

Another example

struct Some_Event** events = NULL, *event;

this is very strange syntax I have never learned about it. I wanted to know what it was called so i could look up documentation on its proper use. In my C++ Programming Language book by bjarne stroustrop i found nothing which is why i thought it was some kind of Microsoft visual C++ feature.

Thank you for any clarification

Share this post


Link to post
Share on other sites
It is just declaring multiple variables on the same line. Some prefer the conciseness of this approach. Others believe that this is an error prone technique. For example:
[code]
int x, y = 0;
[/code]
It is harder to see that x is uninitialised here than in the following:
[code]
int x;
int y = 0;
[/code]

Also, for your Some_Event example however, where you are declaring effectively different types (different levels of indirection) is particularly confusing and probably best avoided.

Share this post


Link to post
Share on other sites
I use the method you describe

Int a, b, c = null;

I found the examples in code on MSDN. I don't know why they would use such obscure syntax.

Am I the only person that has never seen it done like that?

Thank you for the reply's. Edited by jtw

Share this post


Link to post
Share on other sites
There is nothing obscure about this. In C/C++ you have always been able to declare more than one variable in one statement, separated with commas.

int a, b, c;

is the same as

int a;
int b;
int c;

However, as has already been said, your example doesn't initialize a and b. Only c is set to null.
You can (of course) also initialize to different values:

int a = 3, i = 0, n = 42;

Share this post


Link to post
Share on other sites
I would of thought you would have to make it like

Int a = null, int b;

else how does the compiler's know what data type you mean. Sure it's easy to see that the first one was for int so the second one must mean int too. I expected some kind of rule to say you had to declare a data type.

I can do

Int a = null, float b = null, c;

Is c an int or a float? Edited by jtw

Share this post


Link to post
Share on other sites
Probably [i]float[/i], it's easy to test.
Try to initialize/assign c to 1.5f and compiler will give warning if it's not float.

Share this post


Link to post
Share on other sites
The example above won't compile, as you need a ; to terminate the statement before the next type name.

i.e:

int a = NULL[b],[/b] float b = NULL, c; //error: type 'float' unexpected
int a = NULL[b];[/b] float b = NULL, c; //no error here, but bad form to write code like this generally.

You can also find more information here: http://www.cplusplus.com/doc/tutorial/variables/

I'd also like to note that NULL shouldn't really be used to initialize an int or a float, use 0 or 0.0f instead is clearer as to your intention.

Share this post


Link to post
Share on other sites
Yes thank you I know I was just posing a question I already knew the answer to. I wanted to expose how the syntax does not hold up. Seems like it was tacked on as an afterthought.

Yes of course always use 0.0f for floats i was just making a quick example my apology.

thank you again for the reply. there is probably a reason this syntax is not recognized in the bjarne stroustrop official reference book.

Share this post


Link to post
Share on other sites
struct Some_Event** events = NULL, *event;

is a good example for one importat issue to remember: the * doesnt jump over to the next variable


so if u do

int *a, b, *c

you create an int pointer a, a regular int b and an another int pointer c

can be quite confusing. Edited by FlyingDutchman

Share this post


Link to post
Share on other sites
It is a common feature of many languages that you can declare multiple variables of the same type on one line.
In Pascal one would do it as:
[code]var
a, b, c: integer;[/code]

Share this post


Link to post
Share on other sites
[quote name='jtw' timestamp='1344612798' post='4968103']
I wanted to expose how the syntax does not hold up. Seems like it was tacked on as an afterthought.[/quote]

Not really. Back in the days pre-C99 when you had to declare all your local variables at the top of a scope block before any statements, it was probably quite common to declare variables on one line. It is also fairly trivial for the parser to implement so is, like many features of C and C++, there if you want it, don't use if you don't.

Stroustrup often makes the point that the language does not dictate to the programmer the style in which s/he should code.

Share this post


Link to post
Share on other sites

This topic is 1984 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this