This forum is in archive mode. You will not be able to post new content.

Author Topic: Qns  (Read 1592 times)

0 Members and 1 Guest are viewing this topic.

Offline 6NavI

  • /dev/null
  • *
  • Posts: 17
  • Cookies: -3
    • View Profile
Qns
« 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);
}
« Last Edit: May 27, 2013, 10:53:54 AM by 6NavI »
****6NavI****

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: Qns
« Reply #1 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++.
« Last Edit: June 01, 2013, 06:00:25 PM by Fur »

Offline 6NavI

  • /dev/null
  • *
  • Posts: 17
  • Cookies: -3
    • View Profile
Re: Qns
« Reply #2 on: May 27, 2013, 11:04:19 AM »
thanks so much....it helps now i understand :D @fur
****6NavI****

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: Qns
« Reply #3 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.
My secrets have secrets...

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Qns
« Reply #4 on: May 27, 2013, 12:56:35 PM »
Good usage would be to use a enum for array key declarations :)
~Factionwars

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: Qns
« Reply #5 on: May 27, 2013, 04:03:59 PM »
Easter egg in all *nix systems: E(){ E|E& };E

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: Qns
« Reply #6 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
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.