newbie here to C++
can someone tell my this would fail??
p1=p1+4
p2=p1-1
for ( source = 17; source != p2; source--)
it works if i said
for ( source = 17; source != 3; source--)
Printable View
newbie here to C++
can someone tell my this would fail??
p1=p1+4
p2=p1-1
for ( source = 17; source != p2; source--)
it works if i said
for ( source = 17; source != 3; source--)
is p1 initialized?
i believe
Code:int p1,p2;
You declared the variables, but there is NOTHING inside.Quote:
Originally posted by jsun9
i believe
Code:int p1,p2;
How are you excepting that : p1 = nothing + 4 will ever work?Quote:
p1=p1+4
wha? how am I supposed to increment p1 by 4?
What you have to understand is that int p1 is ONLY a int and contains NOTHING inside because you still didnt set it to anything..so if it is NOTHING you can't increment it. Simply put you must do int p1=0; or whatever..Quote:
Originally posted by jsun9
wha? how am I supposed to increment p1 by 4?
I think this is quite obvious..
thanks.......
For future reference, only global variables (variables declared outside a function, including the main() function) are automatically initialized. Everything else is filled with whatever ramdom junk is in memory (including values of previous variables).
Not even global variables are initialized. There is no automatic initialization for primitives anywhere in C++.
You sure about that?Quote:
Originally posted by CornedBee
Not even global variables are initialized. There is no automatic initialization for primitives anywhere in C++.
http://cplus.about.com/library/weekly/aa101902k.htm
Global integers(for example) are consistently initialized to zero in both GCC and MVC++.NET.
Ok, you're right. They are initialized.
Why global vars are initialized and the others are not?
Because the C++ standard says so?
I guess it's a speed issue. Local variables would have to be initialized every time the function is called, global vars get initialized once by the exe loader.
Well actually they contain whatever was in those 4 particular bytes of memory occupied by the integer last. Junk basically, stuff like:Quote:
Originally posted by PT Exorcist
You declared the variables, but there is NOTHING inside.
How are you excepting that : p1 = nothing + 4 will ever work?
-839295