Array

Hey :) So, I have a problem with this code below.... The array does not return what I want but I can't understand why. Thank you :)
1
2
3
4
5
6
7
8
9
10
  int A[2]={};
  int T;

  T=2;

  A[1]=3;
  A[2]=4;

  cout<<A[T];
  
It works for me. Let us know what the error message is...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Example program
#include <iostream>
#include <string>
using namespace std;

int main()
{
  int A[2]={};
  int T;

  T=2;

  A[1]=3;
  A[2]=4;

  cout<<A[T];
  return 0;
  
}
I don't get any error messages but my output is random numbers :/
You are getting An undefined error. The index you are trying to access is out of bounds.
In c++, array index starts from 0 . so, You can only access index from 0 to arraySize - 1In Your Case, your array contains two Elements:
A[0] ==> first element
A[1] ==> Second element

Hope That helps !
Last edited on
You're right but my code still not work :(
ok it works :D thank you all
Topic archived. No new replies allowed.