EvilZone

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

Title: Qns
Post by: 6NavI on 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

Code: [Select]
#include <iostream>
using namespace std;
int navi []={12, 58, 69, 87, 20}
int main () {
int n, a, c;
cout << navi [n];
return (0);
}
Title: Re: Qns
Post by: Fur on 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++.
Title: Re: Qns
Post by: 6NavI on May 27, 2013, 11:04:19 AM
thanks so much....it helps now i understand :D @fur
Title: Re: Qns
Post by: rasenove on May 27, 2013, 11:51:19 AM
Quote from: 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

Code: [Select]
#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.
Title: Re: Qns
Post by: Stackprotector on May 27, 2013, 12:56:35 PM
Good usage would be to use a enum for array key declarations :)
Title: Re: Qns
Post by: s3my0n on May 27, 2013, 04:03:59 PM
(http://lain.ru/shots/navigator.jpg)
Title: Re: Qns
Post by: bluechill on 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

Code: [Select]
#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