2

I tried using below code passing pointer to the array containing the two numbers to be added

#include <stdio.h> 
#include <stdlib.h>  
#include <pthread.h>  

void *print_message_function( void *ptr );  

 main()  
 {  
      pthread_t thread1, thread2;  

      /*const char *message1 = "Thread 1";  

      const char *message2 = "Thread 2"; */

      int arr[2] = {5,8};
      const int *ptrtoarr;
      ptrtoarr=arr;

      int  iret1, iret2;  

     int *s=(int *)(ptrtoarr);
      printf("%d \n", *s); 
      ptrtoarr++;
      s=(int *)(ptrtoarr);
      printf("%d \n", *s); 
     ptrtoarr--;

     /* Create independent threads each of which will execute function */

    iret1 = pthread_create( &thread1, NULL, print_message_function,&arr);  

      if(iret1)  
      {  

          fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1);  
          exit(EXIT_FAILURE);  
      }

    /*  iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);  

      if(iret2)      
      {  
          fprintf(stderr,"Error - pthread_create() return code: %d\n",iret2);  
          exit(EXIT_FAILURE);  
      }  */

      printf("pthread_create() for thread 1 returns: %d\n",iret1);  

     /* printf("pthread_create() for thread 2 returns: %d\n",iret2); */

      /* Wait till threads are complete before main continues. Unless we  */ 
      /* wait we run the risk of executing an exit which will terminate   */ 

      /* the process and all threads before the threads have completed.   */      
      pthread_join( thread1, NULL);  

      /* pthread_join( thread2, NULL); */  

      exit(EXIT_SUCCESS);  
 }  


 void *print_message_function( void *ptr )  
 {  
      /*char *message;  

      message = (char *) ptr;  

      printf("%s \n", message);  */
      printf("\n In message function \n");
      int *sum=(int *)(ptr);
      printf("%d \n", *sum); 
      sum = (int *)(ptr+1);
      printf("%d \n", *sum);


 } 

But in function print_message_function which contains addition steps,using pointer I can access first number but not second one.

(it access both numbers within main function)

Output is as below

5 8 pthread_create() for thread 1 returns: 0

In message function 5 134217728

P.P
  • 117,907
  • 20
  • 175
  • 238
AnupW
  • 21
  • 3

2 Answers2

2

Here,

sum = (int *)(ptr+1);

You are performing pointer arithmetic on a void * (void pointer) which is not allowed in standard C. GCC allows pointer arithmetic on void * by treating its size as 1.

It should be

  sum = (int *)ptr+1;

A cleaner approach would be to use sum itself to get the next element rather than ptr:

void *print_message_function( void *ptr )
{
  printf("\n In message function \n");
  int *sum=ptr;
  printf("%d \n", *sum); 
  sum++;
  printf("%d \n", *sum);
  return NULL;
} 

I removed the cast because void * is compatible with any data pointer. So the cast is needless and added return NULL; as thread function should return a void * or call pthead_exit().

GCC has an option -Wpointer-arith to warn about pointer arithmetic on void pointers.

P.P
  • 117,907
  • 20
  • 175
  • 238
0

AnupW,

Likely your problem stems from using ptr directly in your thread function. It's a void pointer so you don't know what you're going to get. I would refer you to this answer:

Concept of void pointer in C programming

Specifically the answer about pointer arithmetic.

Try casting ptr to an int pointer like you did in main (with ptrtoarr) and using that.

Community
  • 1
  • 1
Michael Albers
  • 3,721
  • 3
  • 21
  • 32