Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

multiple initialization in one line

Posted on 2014-04-07
5
Medium Priority
?
246 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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
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 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 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…

721 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