I was going to try to get you to code thru your problem, but there were several issues, not just the pointer problem. You HAVE to call pthread_wait in main() or the whole process will exit and the thread may or may not have ever executed Client(). So I just made a few changes and let it go with that.
I removed a bunch of intermediate variables, I left error checking up to you. Check return codes. Always.
#include<stdio.h>
#include<string.h>
#include<pthread.h>
void *Client(void *);
typedef
struct {
long int x;
long int y;
} coord_t;
int main()
{
int rc=0;
int *p=&rc;
coord_t my_coord={0,0};
pthread_t threadid;
printf("enter the coordinates for i & j ");
scanf("%ld %ld", &my_coord.x, &my_coord.y);
rc = pthread_create(&threadid, NULL, Client, &my_coord);
pthread_join(threadid, (void **)&p);
return 0;
}
void *Client(void * threadarg)
{
coord_t *p = (coord_t *) threadarg;
printf("%ld ", p->x);
printf("%ld\n", p->y);
pthread_exit(0);
}
With your code was that you were typecasting wrongly.
I only changed typecasting and did some formatting of output. And yes, do follow the practice of returning a correct value, if your function is prototyped to return something ... The very same thing Jim had adviced you too.
#include<stdio.h>
#include<string.h>
#include<pthread.h>
void *Client(void *);
struct coord{
long int x;
long int y;
};
struct coord my_coord;
int main()
{
long int i,j;
int rc;
printf("enter the coordinates : ");
scanf("%ld %ld", &i, &j);
my_coord.x = i;
my_coord.y = j;
pthread_t thread;
rc = pthread_create(&thread,NULL, Client,(void *) &my_coord);
pthread_join(thread, NULL);
return 0;
}
void *Client(void * threadarg)
{
long int xpoint, ypoint;
struct coord *my_point;
my_point = (struct coord *) threadarg;
xpoint = my_point->x;
ypoint = my_point->y;
printf("\n");
printf("%ld\n", xpoint);
printf("%ld\n", ypoint);
return NULL;
}
I'd definitely prefer the Jim's version of codes; however this is what you get as your own running code with the minimum amount of edition. Happy coding .