Syntax of generating an object Data for use as functional argument leading to error












0















We have a typedef *int Data, i am trying to create a generator function



Data data_gen(int idata){
Data d = malloc(sizeof(idata));
d = &idata;
return d;
}


However when I pass this function as argument to another function which requires Data as an argument I recieve the following error, method makes pointer from an integer without a cast, which suggest that my method data_gen is returning an integer as opposed to a data object.



typedef int* Data;


I am calling this function with data_gen and receiving error:



void bstree_insert(BStree bst, Key key, Data data);


where the third argument is call of data_gen










share|improve this question





























    0















    We have a typedef *int Data, i am trying to create a generator function



    Data data_gen(int idata){
    Data d = malloc(sizeof(idata));
    d = &idata;
    return d;
    }


    However when I pass this function as argument to another function which requires Data as an argument I recieve the following error, method makes pointer from an integer without a cast, which suggest that my method data_gen is returning an integer as opposed to a data object.



    typedef int* Data;


    I am calling this function with data_gen and receiving error:



    void bstree_insert(BStree bst, Key key, Data data);


    where the third argument is call of data_gen










    share|improve this question



























      0












      0








      0








      We have a typedef *int Data, i am trying to create a generator function



      Data data_gen(int idata){
      Data d = malloc(sizeof(idata));
      d = &idata;
      return d;
      }


      However when I pass this function as argument to another function which requires Data as an argument I recieve the following error, method makes pointer from an integer without a cast, which suggest that my method data_gen is returning an integer as opposed to a data object.



      typedef int* Data;


      I am calling this function with data_gen and receiving error:



      void bstree_insert(BStree bst, Key key, Data data);


      where the third argument is call of data_gen










      share|improve this question
















      We have a typedef *int Data, i am trying to create a generator function



      Data data_gen(int idata){
      Data d = malloc(sizeof(idata));
      d = &idata;
      return d;
      }


      However when I pass this function as argument to another function which requires Data as an argument I recieve the following error, method makes pointer from an integer without a cast, which suggest that my method data_gen is returning an integer as opposed to a data object.



      typedef int* Data;


      I am calling this function with data_gen and receiving error:



      void bstree_insert(BStree bst, Key key, Data data);


      where the third argument is call of data_gen







      c function pointers






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 4:19







      Ben French

















      asked Nov 22 '18 at 4:01









      Ben FrenchBen French

      84




      84
























          1 Answer
          1






          active

          oldest

          votes


















          0














          This fixes the bug in your generator function:



          Data
          data_gen(int idata)
          {
          Data d = malloc(sizeof(*d));

          *d = idata;

          return d;
          }


          A typedef for this function would be:



          typedef Data (*data_function)(int);


          Then, your tree function would need to be:



          void bstree_insert(BStree bst, Key key, data_function gen);


          So, right now you're mixing up a simple pointer (i.e. int *) with a pointer to a function that returns an int *. These are not the same thing.






          share|improve this answer


























          • ok ill try to fix it thanks for the help!

            – Ben French
            Nov 22 '18 at 4:38











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423693%2fsyntax-of-generating-an-object-data-for-use-as-functional-argument-leading-to-er%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          This fixes the bug in your generator function:



          Data
          data_gen(int idata)
          {
          Data d = malloc(sizeof(*d));

          *d = idata;

          return d;
          }


          A typedef for this function would be:



          typedef Data (*data_function)(int);


          Then, your tree function would need to be:



          void bstree_insert(BStree bst, Key key, data_function gen);


          So, right now you're mixing up a simple pointer (i.e. int *) with a pointer to a function that returns an int *. These are not the same thing.






          share|improve this answer


























          • ok ill try to fix it thanks for the help!

            – Ben French
            Nov 22 '18 at 4:38
















          0














          This fixes the bug in your generator function:



          Data
          data_gen(int idata)
          {
          Data d = malloc(sizeof(*d));

          *d = idata;

          return d;
          }


          A typedef for this function would be:



          typedef Data (*data_function)(int);


          Then, your tree function would need to be:



          void bstree_insert(BStree bst, Key key, data_function gen);


          So, right now you're mixing up a simple pointer (i.e. int *) with a pointer to a function that returns an int *. These are not the same thing.






          share|improve this answer


























          • ok ill try to fix it thanks for the help!

            – Ben French
            Nov 22 '18 at 4:38














          0












          0








          0







          This fixes the bug in your generator function:



          Data
          data_gen(int idata)
          {
          Data d = malloc(sizeof(*d));

          *d = idata;

          return d;
          }


          A typedef for this function would be:



          typedef Data (*data_function)(int);


          Then, your tree function would need to be:



          void bstree_insert(BStree bst, Key key, data_function gen);


          So, right now you're mixing up a simple pointer (i.e. int *) with a pointer to a function that returns an int *. These are not the same thing.






          share|improve this answer















          This fixes the bug in your generator function:



          Data
          data_gen(int idata)
          {
          Data d = malloc(sizeof(*d));

          *d = idata;

          return d;
          }


          A typedef for this function would be:



          typedef Data (*data_function)(int);


          Then, your tree function would need to be:



          void bstree_insert(BStree bst, Key key, data_function gen);


          So, right now you're mixing up a simple pointer (i.e. int *) with a pointer to a function that returns an int *. These are not the same thing.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 4:28

























          answered Nov 22 '18 at 4:18









          Craig EsteyCraig Estey

          14.7k21129




          14.7k21129













          • ok ill try to fix it thanks for the help!

            – Ben French
            Nov 22 '18 at 4:38



















          • ok ill try to fix it thanks for the help!

            – Ben French
            Nov 22 '18 at 4:38

















          ok ill try to fix it thanks for the help!

          – Ben French
          Nov 22 '18 at 4:38





          ok ill try to fix it thanks for the help!

          – Ben French
          Nov 22 '18 at 4:38


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423693%2fsyntax-of-generating-an-object-data-for-use-as-functional-argument-leading-to-er%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen