Solved

multiple initialization in one line

Posted on 2014-04-07
5
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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 34

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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.
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.

752 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