apanimesh061 0 Junior Poster

Here is a program to create two threads in which parent thread read the count of character in inputting string and child thread reverse the string up to that count !

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

struct pass {
   int c;
   char *arr;

void *rev(void *s) {
   printf("In rev, before dec !\n");
   struct pass* p = (struct pass*) s;
   int count, i, j=0;
   char *str, *use;
   count = p->c;
   str = p->arr;
   printf("In rev, after dec, before loop !\n");
   for (i=count-1 ; i>=0; i--) {
      use[j] = str[i]; 
   printf("In rev, after loop !\n");
   str[count] = '\0';

void *count(void *arg) {
   printf("In count, before dec!\n");
   char *str;
   str = (char*) arg;
   int i, l, count;
   pthread_t cthr;
   printf("In count, after dec!\n");   
   struct pass s;
   s.c = count = 0;
   s.arr = str;
   printf("Before pthread cthr !\n");
   pthread_create(&cthr, NULL, rev, &s);
   printf("After pthread cthr !\n");   

   printf("Before pthread cthr join !\n");
   pthread_join(cthr, NULL);
   printf("Before pthread cpthr join !\n");

   while (count != l) {
      count += 1;

int main(void) {
   pthread_t pthr;
   int len;
   char *string = "Animesh Pandey";
   len = strlen(string);
   printf("Before pthread pthr !\n");
   while ()
   pthread_create(&pthr, NULL, count, (void*) string);
   printf("After pthread pthr, before join !\n");   
   pthread_join(pthr, NULL);
   return 0;


Before pthread pthr !
After pthread pthr, before join !
In count, before dec!
In count, after dec!
Before pthread cthr !
After pthread cthr !
Before pthread cthr join !
In rev, before dec !
In rev, after dec, before loop !
In rev, after loop !
Segmentation fault

Am I going right ... ?
Please help !!!!
Thanx :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.