Solved

multiple initialization in one line

Posted on 2014-04-07
5
233 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
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 100 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 33

Accepted Solution

by:
sarabande earned 350 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 33

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

820 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