Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

multiple initialization in one line

Posted on 2014-04-07
5
Medium Priority
?
249 Views
Last Modified: 2014-04-15
Hi
I want to initialize a few variable to a particular value. I want to do this in one line for cleaner code.
For example:

int a,b,c=5;

I am using Visual studio. Only the last variable c seems to be initialized to 5.

Multiple initialization is not possible, or is it? And if so, how?
0
Comment
Question by:LuckyLucks
5 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 200 total points
ID: 39984562
It's not possible like that. Syntactically, you are allowed to assign a value to every varable when declaring it. But since that value is usually differenct for each variable (admit it - your's is not the common case), the compiler will indeed only initialize the last variable. But you can always do something like

int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, w, x, y, z;
a = b = c = d = e = f = g = h = i = j = k = l = m = n = o = p = q = r = s = t = u = w = x = y = z = 5;

Open in new window

0
 
LVL 26

Expert Comment

by:pony10us
ID: 39984589
Not sure about VS but in true batch you can enter multiple commands on one line by separating each with the "&" character.  

Try this from a command line:

set a=5 &set b=5 &set c=5 &echo a = %a% &echo b = %b% &echo c = %c%
0
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 400 total points
ID: 39984608
You're required to one way or another initialize each variable individually.

Now you can do it all on one line:
int a=5, b=5, c=5

Open in new window

or multiple lines like in jkr's example.  But you're still just assigning values individually.  jkr's example just uses the fact that the statement (z = 5) has a resulting value of 5, which can simply be used to feed another (var = value) statement.

Even if you were using arrays or something else, you still have to one way or another initialize each variable individually.  So if you had an array, you would have to do it like this:
int a[10] = { 5,5,5,5,5,5,5,5,5,5};

Open in new window

or you could do it with a loop:
int a[10];
for(int i=0;i<10;i++) a[i] = 5;

Open in new window

Even if you initialize the data using some sort of memcpy, you're still making the assignments one way or another one variable at a time.
0
 
LVL 35

Accepted Solution

by:
sarabande earned 1400 total points
ID: 39985702
To add to before comment:

Now you can do it all on one line:
     int a=5, b=5, c=5; // don't forget the ;

HooKooDooKu's solution is an initialization while other solutions are assignments. that isn't much a difference for c type variables but would be for class type variables where for initialization the constructor was used while for assignments it is operator=.

for cleaner code, initializations probably are the better way. they also have advantages for arrays as also shown by HooKooDooKu.

Sara
0
 
LVL 35

Expert Comment

by:sarabande
ID: 40002584
thanks, but you should turn the accepted solution and the assist (with points). HookooDooKu posted the solution. my comment was an add-on.

Sara
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question