samedi 31 août 2019

Create a list of pointers to random generated int

I'm having some difficulty in generating a random int* and store it into a list<int*>.

I have tried the following:

std::list<int*> generateInt(){
   std::list<int*> randomInt;

   int i = 0;

   // initialize random seed
   srand (time(NULL));

   while (i < 5){
      int* random = (int*)std::rand();
      std::cout << "Random int generated: " << random << std::endl;

      randomInt.push_back(random);
      i++;
   }

   return randomInt;
}

But I get compiler issue as following

error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
       int* random = (int*)std::rand();
                                     ^

I'm not sure if i'm missing something important here? Any help or advice would be very appreciated. Thanks!

Aucun commentaire:

Enregistrer un commentaire