-4

Continue to my last question i am facing one more issue regarding Pass a Vector of structure to a function with void * as in parameter

  #include <iostream>
  #include <vector>
  struct MMT
  {
     int a;
     char b;
     int * data;
  }
  int func(void *structPtr){
     //use the structure member
  }
  int main ()
  {
      std::vector<MMT*> myvector;


      for (int i=1; i<=5; i++){
           MMT *mmt;
           mmt->a = i;
           mmt->b = 'a';
           myvector.push_back(MMT);
      }
      std::cout << "myvector contains:";
      for (std::vector<int*>::iterator it = myvector.begin() ; it !=myvector.end(); ++it)
      {
        func((void*)it);//?????????//how to pass structure
      }
      std::cout << '\n';
      return 0;
  }

I am getting Null pointer error

Akankshi Mishra
  • 65
  • 2
  • 10
  • *Why* would you want to use a `void*` instead of, say, just passing a `const std::vector&`? – Jesper Juhl May 02 '18 at 08:05
  • There are a lot of concerns here - why are you trying to treat `MMT*`s as `int*`s, then `void*`s, then back to `MMT*`s?!?! Just stick to the type the object actually is! – BoBTFish May 02 '18 at 08:05
  • There are multiple *other* errors in that code that you should fix first. And once you've done that, it's just a question of applying the address-of and dereference operators correctly. – Some programmer dude May 02 '18 at 08:05
  • It looks like you're trying to learn about pointers through trial and error. Use [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead. – molbdnilo May 02 '18 at 08:06
  • 3
    And, ***why*** do you have a function taking a `void*` as argument? You really should not do that in C++. If you want a "generic" function that can takes many different types, use templates. If the function is supposed to only take the structure as argument, use the proper structure type. – Some programmer dude May 02 '18 at 08:06
  • There is literally no excuse for using void *. Even in c I would argue that its use is a sign of bad design. If you want to be able to take multiple arguments to a function, don't, and make multiple functions (templates get compiled to multiple functions, and so count) – UKMonkey May 02 '18 at 08:08
  • in c you are some times forces to use void* but in c++ i fully agree and this case wasn't a good use of void* – Tyker May 02 '18 at 08:14
  • This is just a sample code i have provided here the code is just a prototype, fuction with void*ptr is actually my library function which i am using for a specific task. – Akankshi Mishra May 02 '18 at 08:20

1 Answers1

0

There are multiple issues with your code like you are not allocating memory and secondly it itself is a iterator/pointer, so you need to change ur code as follows :

  #include <iostream>
  #include <vector>
  #include <memory>
  struct MMT
  {
     int a;
     char b;
     int * data;
  };
  int func(void *structPtr){
     //use the structure member
  }
  int main ()
  {
      //Better to use smart pointers so no need to manage memory
      std::vector<std::unique_ptr<MMT>> myvector;


      for (int i=1; i<=5; i++){

           myvector.push_back(std::unique_ptr<MMT>(new MMT{i,'a'}));
      }
      std::cout << "myvector contains:";
      for (std::vector<std::unique_ptr<MMT>>::iterator it = myvector.begin() ; it !=myvector.end(); ++it)
      {
        func((void*)(it->get()));
      }
      std::cout << '\n';
      return 0;
  }
PapaDiHatti
  • 1,841
  • 19
  • 26
  • @Tyker this is just sample code and i added comment at end for OP so that he call delete on actual code but it's good idea to demonstrate smart pointer usage here, i will add – PapaDiHatti May 02 '18 at 08:15
  • i understand but showing new without delete isn't good for OP – Tyker May 02 '18 at 08:20
  • Thanks for pointing the memory leaks, i know i just provided a code which is not a actual one for which i am working and yes i have already taken case of MMT *mmt = new MMT(); and deallocation of memory – Akankshi Mishra May 02 '18 at 08:21