Arrays return different results for the same indexes

I was trying to make a multidimensional array implementing pointers. However, the SAME array produces two different results two times I access it : breakpoint 1, breakpoint 2. Could someone help, please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 /******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <iostream>
#include <stdio.h>


using namespace std;

int
main ()
{


  //T is the number of continents
  //each continent has a number of A-B populations (for example A-B (continent 1), A2-B2 (continent 2)
  int T = 0;
  cout << "How many continents are there on your planet?" << endl;
  cin >> T;
  cout << "You have input  " << T << "  continents" << endl << endl;

  //these arrys will hold the masses of A animals on each continent
  //for example A[0] and array of masses of animals on the first continents
  int *A[T];
  int *B[T];

  //number of animals in each population (on each continent - a counter)    
  int A_n[T];
  int B_n[T];

  int pairsCount[T];
  int pairs = 0;

  //cin the information
  for (int i = 0; i < T; i++)
    {
      cout << "Let's talk about the " << T << "  population" << endl;
      cout << "How many animals are there? " << endl;

      cout << "The number of A animals on the " << i + 1 << " continent " << endl;
      cin >> A_n[i];

      cout << "Please write the masses of the A animals" << endl;
      int a_sub[A_n[i]];

      for (int a = 0; a < A_n[i]; a++)
	{
	  cin >> a_sub[a];
	}

      cout << "The number of B animals on the " << i + 1 << " continent " << endl;
      cin >> B_n[i];

      cout << "Please write the masses of the B animals" << endl;
      int b_sub[B_n[i]];

      for (int a = 0; a < B_n[i]; a++)
	{
	  cin >> b_sub[a];
	}

      //trying to store the pointers here in the corresponding arrays
      //the idea is to put the array with masses into a bigger array
      A[i] = &a_sub[0];
      B[i] = &b_sub[0];

   //breakpoint 1     
    cout << A[0][0];
    cout << A[0][1];
    cout << A[0][2] << endl;
    
        
    }
    //breakpoint 2
    cout << A[0][0];
    cout << A[0][1];
    cout << A[0][2];
    
}


int a_sub[A_n[i]]; and int b_sub[B_n[i]];
are illegal.

1
2
  int *A[T];
  int *B[T];

is also illegal.

Did the program compile? That seems very wrong.

Anyways,
1
2
  int* A[10];
  int* B[10];


Declares two static arrays A and B having 10 indexes each which are arrays of pointers to integers. The indexes cannot be used but they can be assigned an address value.

The correct way to declare a 2D array using pointers would be using "new" and "delete" (read dynamic allocation) and pointers to pointers.

1
2
3
int* a = new int[NO_OF_ROWS];
for(int i=0; i<NO_OF_ROWS; i++)
   a[i] = new int[NO_OF_COLUMNS];


Last edited on
Thank you for the answer. Yes, it compiled.

I tried a number of simpler codes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

int main()
{

int *A [3];
int a1[5]= {1,2,3,4,5};
int a2[5]= {6,7,8,9,10};
int a3[5]= {11,12,13,13,15};

A[0] = &a1[0];
A[1] = &a2[0];
A[2] = &a2[0];

cout << A[1][3];


}


This one worked just fine.

In your example,

1
2
3
int* a = new int[NO_OF_ROWS];
for(int i=0; i<NO_OF_ROWS; i++)
   a[i] = new int[NO_OF_COLUMNS];


How do I access the number itself?
a[0][1]

Also, I might have misused the word 2D. What I actually need is to store an array in an array: so that each element of one array is an array of itself.
Last edited on
Yes, it compiled.
Runtime sized arrays are an extension supported by some compilers but not part of the standard. If you want your code to be portable, use vectors instead.

But that won't help with the original problem, which is that a_sub and b_sub go out of scope at line 78. So lines 80-82 print out whatever bits the program happens to have where a_sub and b_sub were formerly stored.

To fix this you'd have to allocate the a_sub and b_sub arrays on the heap instead, and then remember to delete them all when you're done. These are more reasons to use vectors instead.
Topic archived. No new replies allowed.