Solved

multiple initialization in one line

Posted on 2014-04-07
5
227 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 50 total points
Comment Utility
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
Comment Utility
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 100 total points
Comment Utility
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 32

Accepted Solution

by:
sarabande earned 350 total points
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now