-4

In my code i am trying to have the members of an int pointer-array point to the members of the int array in my struct Bucket. The problem i get is that when running the program, i can copy the first 3 members, but as i try to copy the 4th, i get a runtime error. Why is this? Am I setting the pointers right? What should I do differently?

#include <iostream>
int main()
{
    struct Bucket 
    {
         int values[10];
         Bucket()
         {
            n=0; 
            t=0;
         }
    };

    int* Ordnung[10];
    Bucket Neuerbucket;

    for (int i=0; i<10; i++)
        Neuerbucket.values[i]=i;

    for (int i=0; i<10;i++)
        std::cout<< Neuerbucket.values[i];

    std:: cout<< '\n';
    int* array[10];

    for (int j=0; j<10; j++)
    {
        *array[j]=(Neuerbucket.values[j]);
        std:: cout<<*array[j];
    }
    std:: cout << "TROLOL"; 

    return 1;
}

NOTE: This is not a program that should do anything, it is just for the better understanding of pointers and classes/structs. The knowledge I hope to get through your answers will hopefully help me to program my data structure program. Thanks in advance, you're the best, J.K. :)

Kiroxas
  • 891
  • 10
  • 24

1 Answers1

2

This line:

*array[j]=(Neuerbucket.values[j]);

Should probably be:

array[j]=&Neuerbucket.values[j];

Otherwise you're putting int values from Neuerbucket wherever the uninitialized pointers of array point.

Quentin
  • 62,093
  • 7
  • 131
  • 191