How to use a struct pointer returned to the in the main from a thread function?What should main() return in C and C++?How do function pointers in C work?Is it possible to iterate over arguments in variadic macros?Program run in child process doesn't loopIn a c program, does the whole process terminates when the main thread terminate?pointer to array of structmalloc and pointer in a structHow to drain the java thread stack memory area?C Pass arguments as void-pointer-list to imported function from LoadLibrary()how to create a struct and pass to a new thread via pthread_create()

The economics of a "no deal" Brexit

How to retract the pitched idea from employer?

siunitx error: Invalid numerical input

How bad would a partial hash leak be, realistically?

What's the correct term for a waitress in the Middle Ages?

Secure offsite backup, even in the case of hacker root access

When writing an error prompt, should we end the sentence with a exclamation mark or a dot?

About the expansion of seq_set_split

How to skip replacing first occurrence of a character in each line?

How were concentration and extermination camp guards recruited?

Did Darth Vader wear the same suit for 20+ years?

Disclosing Spiritual Experiences

What LISP compilers and interpreters were available for 8-bit machines?

Does the "6 seconds per round" rule apply to speaking/roleplaying during combat situations?

After the loss of Challenger, why weren’t Galileo and Ulysses launched by Centaurs on expendable boosters?

Is it recommended against to open-source the code of a webapp?

Why does the Schrödinger equation work so well for the Hydrogen atom despite the relativistic boundary at the nucleus?

How to make a setting relevant?

Java guess the number

Why does Kathryn say this in 12 Monkeys?

Select items in a list that contain criteria #2

Bent spoke design wheels — feasible?

How did students remember what to practise between lessons without any sheet music?

Select items in a list that contain criteria



How to use a struct pointer returned to the in the main from a thread function?


What should main() return in C and C++?How do function pointers in C work?Is it possible to iterate over arguments in variadic macros?Program run in child process doesn't loopIn a c program, does the whole process terminates when the main thread terminate?pointer to array of structmalloc and pointer in a structHow to drain the java thread stack memory area?C Pass arguments as void-pointer-list to imported function from LoadLibrary()how to create a struct and pass to a new thread via pthread_create()






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am writing a program that takes integers as command line arguments. For each of these numbers I have to create a thread which calculates Fibonacci series up to that number. That function returns a struct pointer to the main where the data is printed.



Now, I have correctly done the fib calculations and checked them by printing the series within the function.



The problem arises when I try to return the struct pointer created within the thread function and use it to print the data in the main.



typedef struct thread_func_param

int *fib;
int size;
thread_func_param;
//===================================================
void *fibGen(void *parameters)

int num = atoi(parameters);
struct thread_func_param *p;
p = malloc (sizeof (thread_func_param));

p->size = fibSize(num);
p->fib = malloc(sizeof(int)* p->size);

//Fibonacci Calculations
//..
//.

return (void *) p;
//pthread_exit((void *) p);

//===================================================
int main(int argc, char* argv[])

void* thread_result;
thread_func_param* p = malloc( sizeof(thread_func_param));
assert(argc > 1);

int noOfThreads = argc - 1;
printf("No of Thread = %dn", noOfThreads);
pthread_t *threadID = malloc (sizeof (pthread_t) * noOfThreads);

pthread_attr_t attributes;
pthread_attr_init(&attributes);

int i, j;
for(i = 0; i < noOfThreads; i++)

pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);
pthread_join(threadID[i], thread_result);


//HOW TO USE THE RETURNED DATA?
for (j = 0; j< ((thread_func_param*)thread_result->size)-1; j++)
printf(" %d ", (thread_func_param*)thread_result->fib[j]);


return 0;



The solution that I use, in the end, to print the data gives error of dereferencing a void pointer (I am new with C). How can I correct it?










share|improve this question






























    0















    I am writing a program that takes integers as command line arguments. For each of these numbers I have to create a thread which calculates Fibonacci series up to that number. That function returns a struct pointer to the main where the data is printed.



    Now, I have correctly done the fib calculations and checked them by printing the series within the function.



    The problem arises when I try to return the struct pointer created within the thread function and use it to print the data in the main.



    typedef struct thread_func_param

    int *fib;
    int size;
    thread_func_param;
    //===================================================
    void *fibGen(void *parameters)

    int num = atoi(parameters);
    struct thread_func_param *p;
    p = malloc (sizeof (thread_func_param));

    p->size = fibSize(num);
    p->fib = malloc(sizeof(int)* p->size);

    //Fibonacci Calculations
    //..
    //.

    return (void *) p;
    //pthread_exit((void *) p);

    //===================================================
    int main(int argc, char* argv[])

    void* thread_result;
    thread_func_param* p = malloc( sizeof(thread_func_param));
    assert(argc > 1);

    int noOfThreads = argc - 1;
    printf("No of Thread = %dn", noOfThreads);
    pthread_t *threadID = malloc (sizeof (pthread_t) * noOfThreads);

    pthread_attr_t attributes;
    pthread_attr_init(&attributes);

    int i, j;
    for(i = 0; i < noOfThreads; i++)

    pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);
    pthread_join(threadID[i], thread_result);


    //HOW TO USE THE RETURNED DATA?
    for (j = 0; j< ((thread_func_param*)thread_result->size)-1; j++)
    printf(" %d ", (thread_func_param*)thread_result->fib[j]);


    return 0;



    The solution that I use, in the end, to print the data gives error of dereferencing a void pointer (I am new with C). How can I correct it?










    share|improve this question


























      0












      0








      0


      1






      I am writing a program that takes integers as command line arguments. For each of these numbers I have to create a thread which calculates Fibonacci series up to that number. That function returns a struct pointer to the main where the data is printed.



      Now, I have correctly done the fib calculations and checked them by printing the series within the function.



      The problem arises when I try to return the struct pointer created within the thread function and use it to print the data in the main.



      typedef struct thread_func_param

      int *fib;
      int size;
      thread_func_param;
      //===================================================
      void *fibGen(void *parameters)

      int num = atoi(parameters);
      struct thread_func_param *p;
      p = malloc (sizeof (thread_func_param));

      p->size = fibSize(num);
      p->fib = malloc(sizeof(int)* p->size);

      //Fibonacci Calculations
      //..
      //.

      return (void *) p;
      //pthread_exit((void *) p);

      //===================================================
      int main(int argc, char* argv[])

      void* thread_result;
      thread_func_param* p = malloc( sizeof(thread_func_param));
      assert(argc > 1);

      int noOfThreads = argc - 1;
      printf("No of Thread = %dn", noOfThreads);
      pthread_t *threadID = malloc (sizeof (pthread_t) * noOfThreads);

      pthread_attr_t attributes;
      pthread_attr_init(&attributes);

      int i, j;
      for(i = 0; i < noOfThreads; i++)

      pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);
      pthread_join(threadID[i], thread_result);


      //HOW TO USE THE RETURNED DATA?
      for (j = 0; j< ((thread_func_param*)thread_result->size)-1; j++)
      printf(" %d ", (thread_func_param*)thread_result->fib[j]);


      return 0;



      The solution that I use, in the end, to print the data gives error of dereferencing a void pointer (I am new with C). How can I correct it?










      share|improve this question
















      I am writing a program that takes integers as command line arguments. For each of these numbers I have to create a thread which calculates Fibonacci series up to that number. That function returns a struct pointer to the main where the data is printed.



      Now, I have correctly done the fib calculations and checked them by printing the series within the function.



      The problem arises when I try to return the struct pointer created within the thread function and use it to print the data in the main.



      typedef struct thread_func_param

      int *fib;
      int size;
      thread_func_param;
      //===================================================
      void *fibGen(void *parameters)

      int num = atoi(parameters);
      struct thread_func_param *p;
      p = malloc (sizeof (thread_func_param));

      p->size = fibSize(num);
      p->fib = malloc(sizeof(int)* p->size);

      //Fibonacci Calculations
      //..
      //.

      return (void *) p;
      //pthread_exit((void *) p);

      //===================================================
      int main(int argc, char* argv[])

      void* thread_result;
      thread_func_param* p = malloc( sizeof(thread_func_param));
      assert(argc > 1);

      int noOfThreads = argc - 1;
      printf("No of Thread = %dn", noOfThreads);
      pthread_t *threadID = malloc (sizeof (pthread_t) * noOfThreads);

      pthread_attr_t attributes;
      pthread_attr_init(&attributes);

      int i, j;
      for(i = 0; i < noOfThreads; i++)

      pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);
      pthread_join(threadID[i], thread_result);


      //HOW TO USE THE RETURNED DATA?
      for (j = 0; j< ((thread_func_param*)thread_result->size)-1; j++)
      printf(" %d ", (thread_func_param*)thread_result->fib[j]);


      return 0;



      The solution that I use, in the end, to print the data gives error of dereferencing a void pointer (I am new with C). How can I correct it?







      c pthreads pthread-join






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 24 at 16:17







      Robin Goodfellow

















      asked Mar 24 at 15:18









      Robin GoodfellowRobin Goodfellow

      218




      218






















          1 Answer
          1






          active

          oldest

          votes


















          2














          Two issues here:




          1. pthread_join() takes a void** as 2nd parameter. The code passes a void* only.


          2. To cast a pointer wrap it into parenthesis. Here the cast



            (thread_func_param*)thread_result->size


            refers to size not to thread_result. So what you want is



            ((thread_func_param*)thread_result)->size


          However a nice and clean solution would only use a void pointer interimswise. It could look like this:



          int main(int argc, char* argv[])
          {
          thread_func_param* thread_result;

          ...

          ...

          pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);


          void * pv;
          pthread_join(threadID[i], &pv);
          thread_result = pv;


          if (NULL != thread_result) /* perform some sanity checking. */

          for (j = 0; j < thread_result->size - 1; j++)
          printf(" %d ", thread_result->fib[j]);


          ...





          share|improve this answer

























          • Corrected the code. Now, it gives a segmentation fault.

            – Robin Goodfellow
            Mar 24 at 15:38











          • In which line does the code end with a segmentation violation?

            – alk
            Mar 24 at 15:41











          • In gdb, it is on the line where I declare the for loop. It cannot match the condition for i.

            – Robin Goodfellow
            Mar 24 at 15:48











          • Thanks a lot! It finally worked.

            – Robin Goodfellow
            Mar 24 at 15:58






          • 1





            Of course I was going to do it; I was making some changes in my code.

            – Robin Goodfellow
            Mar 24 at 16:12











          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%2f55325278%2fhow-to-use-a-struct-pointer-returned-to-the-in-the-main-from-a-thread-function%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









          2














          Two issues here:




          1. pthread_join() takes a void** as 2nd parameter. The code passes a void* only.


          2. To cast a pointer wrap it into parenthesis. Here the cast



            (thread_func_param*)thread_result->size


            refers to size not to thread_result. So what you want is



            ((thread_func_param*)thread_result)->size


          However a nice and clean solution would only use a void pointer interimswise. It could look like this:



          int main(int argc, char* argv[])
          {
          thread_func_param* thread_result;

          ...

          ...

          pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);


          void * pv;
          pthread_join(threadID[i], &pv);
          thread_result = pv;


          if (NULL != thread_result) /* perform some sanity checking. */

          for (j = 0; j < thread_result->size - 1; j++)
          printf(" %d ", thread_result->fib[j]);


          ...





          share|improve this answer

























          • Corrected the code. Now, it gives a segmentation fault.

            – Robin Goodfellow
            Mar 24 at 15:38











          • In which line does the code end with a segmentation violation?

            – alk
            Mar 24 at 15:41











          • In gdb, it is on the line where I declare the for loop. It cannot match the condition for i.

            – Robin Goodfellow
            Mar 24 at 15:48











          • Thanks a lot! It finally worked.

            – Robin Goodfellow
            Mar 24 at 15:58






          • 1





            Of course I was going to do it; I was making some changes in my code.

            – Robin Goodfellow
            Mar 24 at 16:12















          2














          Two issues here:




          1. pthread_join() takes a void** as 2nd parameter. The code passes a void* only.


          2. To cast a pointer wrap it into parenthesis. Here the cast



            (thread_func_param*)thread_result->size


            refers to size not to thread_result. So what you want is



            ((thread_func_param*)thread_result)->size


          However a nice and clean solution would only use a void pointer interimswise. It could look like this:



          int main(int argc, char* argv[])
          {
          thread_func_param* thread_result;

          ...

          ...

          pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);


          void * pv;
          pthread_join(threadID[i], &pv);
          thread_result = pv;


          if (NULL != thread_result) /* perform some sanity checking. */

          for (j = 0; j < thread_result->size - 1; j++)
          printf(" %d ", thread_result->fib[j]);


          ...





          share|improve this answer

























          • Corrected the code. Now, it gives a segmentation fault.

            – Robin Goodfellow
            Mar 24 at 15:38











          • In which line does the code end with a segmentation violation?

            – alk
            Mar 24 at 15:41











          • In gdb, it is on the line where I declare the for loop. It cannot match the condition for i.

            – Robin Goodfellow
            Mar 24 at 15:48











          • Thanks a lot! It finally worked.

            – Robin Goodfellow
            Mar 24 at 15:58






          • 1





            Of course I was going to do it; I was making some changes in my code.

            – Robin Goodfellow
            Mar 24 at 16:12













          2












          2








          2







          Two issues here:




          1. pthread_join() takes a void** as 2nd parameter. The code passes a void* only.


          2. To cast a pointer wrap it into parenthesis. Here the cast



            (thread_func_param*)thread_result->size


            refers to size not to thread_result. So what you want is



            ((thread_func_param*)thread_result)->size


          However a nice and clean solution would only use a void pointer interimswise. It could look like this:



          int main(int argc, char* argv[])
          {
          thread_func_param* thread_result;

          ...

          ...

          pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);


          void * pv;
          pthread_join(threadID[i], &pv);
          thread_result = pv;


          if (NULL != thread_result) /* perform some sanity checking. */

          for (j = 0; j < thread_result->size - 1; j++)
          printf(" %d ", thread_result->fib[j]);


          ...





          share|improve this answer















          Two issues here:




          1. pthread_join() takes a void** as 2nd parameter. The code passes a void* only.


          2. To cast a pointer wrap it into parenthesis. Here the cast



            (thread_func_param*)thread_result->size


            refers to size not to thread_result. So what you want is



            ((thread_func_param*)thread_result)->size


          However a nice and clean solution would only use a void pointer interimswise. It could look like this:



          int main(int argc, char* argv[])
          {
          thread_func_param* thread_result;

          ...

          ...

          pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);


          void * pv;
          pthread_join(threadID[i], &pv);
          thread_result = pv;


          if (NULL != thread_result) /* perform some sanity checking. */

          for (j = 0; j < thread_result->size - 1; j++)
          printf(" %d ", thread_result->fib[j]);


          ...






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 24 at 16:04

























          answered Mar 24 at 15:31









          alkalk

          60.1k868180




          60.1k868180












          • Corrected the code. Now, it gives a segmentation fault.

            – Robin Goodfellow
            Mar 24 at 15:38











          • In which line does the code end with a segmentation violation?

            – alk
            Mar 24 at 15:41











          • In gdb, it is on the line where I declare the for loop. It cannot match the condition for i.

            – Robin Goodfellow
            Mar 24 at 15:48











          • Thanks a lot! It finally worked.

            – Robin Goodfellow
            Mar 24 at 15:58






          • 1





            Of course I was going to do it; I was making some changes in my code.

            – Robin Goodfellow
            Mar 24 at 16:12

















          • Corrected the code. Now, it gives a segmentation fault.

            – Robin Goodfellow
            Mar 24 at 15:38











          • In which line does the code end with a segmentation violation?

            – alk
            Mar 24 at 15:41











          • In gdb, it is on the line where I declare the for loop. It cannot match the condition for i.

            – Robin Goodfellow
            Mar 24 at 15:48











          • Thanks a lot! It finally worked.

            – Robin Goodfellow
            Mar 24 at 15:58






          • 1





            Of course I was going to do it; I was making some changes in my code.

            – Robin Goodfellow
            Mar 24 at 16:12
















          Corrected the code. Now, it gives a segmentation fault.

          – Robin Goodfellow
          Mar 24 at 15:38





          Corrected the code. Now, it gives a segmentation fault.

          – Robin Goodfellow
          Mar 24 at 15:38













          In which line does the code end with a segmentation violation?

          – alk
          Mar 24 at 15:41





          In which line does the code end with a segmentation violation?

          – alk
          Mar 24 at 15:41













          In gdb, it is on the line where I declare the for loop. It cannot match the condition for i.

          – Robin Goodfellow
          Mar 24 at 15:48





          In gdb, it is on the line where I declare the for loop. It cannot match the condition for i.

          – Robin Goodfellow
          Mar 24 at 15:48













          Thanks a lot! It finally worked.

          – Robin Goodfellow
          Mar 24 at 15:58





          Thanks a lot! It finally worked.

          – Robin Goodfellow
          Mar 24 at 15:58




          1




          1





          Of course I was going to do it; I was making some changes in my code.

          – Robin Goodfellow
          Mar 24 at 16:12





          Of course I was going to do it; I was making some changes in my code.

          – Robin Goodfellow
          Mar 24 at 16:12



















          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%2f55325278%2fhow-to-use-a-struct-pointer-returned-to-the-in-the-main-from-a-thread-function%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현