EvilZone

Programming and Scripting => C - C++ => : 6NavI May 27, 2013, 10:41:47 AM

: Qns
: 6NavI May 27, 2013, 10:41:47 AM
Hey Ez
can some one help me on what this code means ...like break it down for because to me it doesn't make sense...why does it print out 58? Is it that "navi [n]" is equivalent to "navi [1]" and if so how come???

here is the code

:
#include <iostream>
using namespace std;
int navi []={12, 58, 69, 87, 20}
int main () {
int n, a, c;
cout << navi [n];
return (0);
}
: Re: Qns
: Fur May 27, 2013, 10:56:47 AM
Outputs 12 (as expected) for me.
Running it in Dev-C++ 5.4.1.

I'm not a C++ coder, but it seems that undefined variabled are equal to null. I think int is a non-nullable type, so it uses 0 instead.

Also, you missed a semicolon at the end of line 3.


P.S: I know nothing about C++.
: Re: Qns
: 6NavI May 27, 2013, 11:04:19 AM
thanks so much....it helps now i understand :D @fur
: Re: Qns
: rasenove May 27, 2013, 11:51:19 AM
: 6NavI
link=topic=10504.msg58056#msg58056 date=1369644107]
Hey Ez
can some one help me on what this code means ...like break it down for because to me it doesn't make sense...why does it print out 58? Is it that "navi [n]" is equivalent to "navi [1]" and if so how come???

here is the code

:
#include <iostream>
using namespace std;
int navi []={12, 58, 69, 87, 20}
int main () {
int n, a, c;
cout << navi [n];
return (0);
}

in standard C++, arry elements start from 0, so navi[0] is 12 and navi[1] is 58.
: Re: Qns
: Stackprotector May 27, 2013, 12:56:35 PM
Good usage would be to use a enum for array key declarations :)
: Re: Qns
: s3my0n May 27, 2013, 04:03:59 PM
(http://lain.ru/shots/navigator.jpg)
: Re: Qns
: bluechill May 27, 2013, 04:33:21 PM
Hey Ez
can some one help me on what this code means ...like break it down for because to me it doesn't make sense...why does it print out 58? Is it that "navi [n]" is equivalent to "navi [1]" and if so how come???

here is the code

:
#include <iostream>
using namespace std;
int navi []={12, 58, 69, 87, 20}
int main () {
int n, a, c;
cout << navi [n];
return (0);
}

An uninitialized variable is not necessarily 0 or null.  In windows compiled with vs c++ this would be navi[0xDDDDDD] which wouldn't be valid.....  *always* initialize your variables