Jump to content

  • Log In with Google      Sign In   
  • Create Account

c++ initiliazation what do you call it


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 jtw   Members   -  Reputation: 113

Like
0Likes
Like

Posted 10 August 2012 - 06:11 AM

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.

Sponsor:

#2 Ripiz   Members   -  Reputation: 529

Like
2Likes
Like

Posted 10 August 2012 - 06:38 AM

What's your question?

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

#3 jtw   Members   -  Reputation: 113

Like
0Likes
Like

Posted 10 August 2012 - 06:50 AM

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

#4 rip-off   Moderators   -  Reputation: 8210

Like
4Likes
Like

Posted 10 August 2012 - 07:02 AM

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:
int x, y = 0;
It is harder to see that x is uninitialised here than in the following:
int x;
int y = 0;

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.

#5 jtw   Members   -  Reputation: 113

Like
0Likes
Like

Posted 10 August 2012 - 07:17 AM

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, 10 August 2012 - 07:24 AM.


#6 SamiHuutoniemi   Members   -  Reputation: 259

Like
2Likes
Like

Posted 10 August 2012 - 07:36 AM

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;

#7 jtw   Members   -  Reputation: 113

Like
0Likes
Like

Posted 10 August 2012 - 08:00 AM

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, 10 August 2012 - 08:01 AM.


#8 Ripiz   Members   -  Reputation: 529

Like
0Likes
Like

Posted 10 August 2012 - 08:17 AM

Probably float, it's easy to test.
Try to initialize/assign c to 1.5f and compiler will give warning if it's not float.

#9 dougbinks   Members   -  Reputation: 484

Like
3Likes
Like

Posted 10 August 2012 - 08:37 AM

The example above won't compile, as you need a ; to terminate the statement before the next type name.

i.e:

int a = NULL, float b = NULL, c; //error: type 'float' unexpected
int a = NULL; 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.

#10 jtw   Members   -  Reputation: 113

Like
-2Likes
Like

Posted 10 August 2012 - 09:33 AM

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.

#11 FlyingDutchman   Members   -  Reputation: 207

Like
3Likes
Like

Posted 10 August 2012 - 10:30 AM

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, 10 August 2012 - 10:31 AM.

I open sourced my C++/iOS OpenGL 2D RPG engine :-)


See my blog: (Tutorials and GameDev)

http://howtomakeitin....wordpress.com/


#12 Bregma   Crossbones+   -  Reputation: 5133

Like
5Likes
Like

Posted 10 August 2012 - 12:56 PM

there is probably a reason this syntax is not recognized in the bjarne stroustrop official reference book.

Yes, indeed. Professor Stroustrup prefers to convey only good practices and beneficial uses of the C++ programming language. In fact, he does discuss the multiple-declaration syntax in my copy of his book "The C++ Programming Language" and recommends against its use for all the same reasons documented here.
Stephen M. Webb
Professional Free Software Developer

#13 iMalc   Crossbones+   -  Reputation: 2306

Like
0Likes
Like

Posted 10 August 2012 - 11:26 PM

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:
var
  a, b, c: integer;

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

#14 Aardvajk   Crossbones+   -  Reputation: 5979

Like
1Likes
Like

Posted 11 August 2012 - 03:42 AM

I wanted to expose how the syntax does not hold up. Seems like it was tacked on as an afterthought.


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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS