Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8595249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:23:25+00:00 2026-06-12T00:23:25+00:00

#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <stdint.h> #include <inttypes.h> typedef struct tmp_num{ int

  • 0
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdint.h>
#include <inttypes.h>

typedef struct tmp_num{
    int tmp_1;
    int tmp_2;
}t_num;

/* Dichiarazione prototipi secondo le direttive di pthread */
void *num_mezzo_1(void *num_orig);
void *num_mezzo_2(void *num_orig);

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

    /* Inizializzo i 2 thread */
    pthread_t thread1, thread2;
    int tmp=0,rc1,rc2,num;

    t_num main_tnum;
    num=atoi(argv[1]);
    if(num <= 3){
        printf("Questo è un numero primo: %d\n", num);
        exit(0);
    }
    if( (rc1=pthread_create( &thread1, NULL, &num_mezzo_1, (void *)num)) ){
        printf("Creazione del thread fallita: %d\n", rc1);
        exit(1);
    }
    if( (rc2=pthread_create( &thread2, NULL, &num_mezzo_2, (void *)num)) ){
            printf("Creazione del thread fallita: %d\n", rc2);
            exit(1);
    }
    main_tnum.tmp_1 = 0;
    main_tnum.tmp_2 = 0;
    pthread_join(thread1, (void **)&(main_tnum.tmp_1));
    pthread_join(thread2, (void **)&(main_tnum.tmp_2));
    tmp=main_tnum.tmp_1+main_tnum.tmp_2;
    if(tmp>2){
        printf("Questo NON è un numero primo: %d\n", num);
    }
    else{
        printf("Questo è un numero primo: %d\n", num);
    }
    exit(0);
}

void *num_mezzo_1(void *num_orig){
    t_num p1_tnum;
    int cont_1;
    int n_orig=(int)num_orig;
    p1_tnum.tmp_1 = 0;
    for(cont_1=1; cont_1<=(n_orig/2); cont_1++){
        if((n_orig % cont_1) == 0){
            (p1_tnum.tmp_1)++;
        }
    }
    pthread_exit((void *)(p1_tnum.tmp_1));
    return NULL;
}

void *num_mezzo_2(void *num_orig){
    t_num p2_tnum;
    int cont_2;
    int n_orig=(int)num_orig;
    p2_tnum.tmp_2 = 0;
    for(cont_2=((n_orig/2)+1); cont_2<=n_orig; cont_2++){
        if((n_orig % cont_2) == 0){
            (p2_tnum.tmp_2)++;
        }
    }
    pthread_exit((void *)(p2_tnum.tmp_2));
    return NULL;
}

I’m using Debian Sid 64bit and i have a problem with this simple program that i have developed.
If i compile with clang -Wall -Wextra -o test prime.c -lpthread i got only 1 warn (unused argc) and the program runs fine.
But if i compile it with GCC-4.7.2 (gcc -Wall -Wextra -o test prime.c -lpthread) i got more warns (unused argc + various cast from pointer to integer of different size) and the program fails with a SegFault.
I don’t understand why with clang it works and with GCC it doesn’t.

EDIT:

num_primo_pthread.c: In function ‘main’:
num_primo_pthread.c:33:57: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
num_primo_pthread.c:37:57: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
num_primo_pthread.c:16:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
num_primo_pthread.c: In function ‘num_mezzo_1’:
num_primo_pthread.c:61:13: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
num_primo_pthread.c:68:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
num_primo_pthread.c: In function ‘num_mezzo_2’:
num_primo_pthread.c:75:13: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
num_primo_pthread.c:82:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:23:26+00:00Added an answer on June 12, 2026 at 12:23 am

    the problem lies in this line

    int n_orig=(int)num_orig;
    

    num_orig is a pointer to an int, not an int. replace with:

    int n_orig=(int)*num_orig;
    

    Edit:

    The problem lies a little bit deeper:

    (void *)num
    

    you can’t do that, pointers are not used that way, change it to

    (void *)&num
    

    Second edit after viewing the errors:

    You are doing the same with the return function

    (void *)(p2_tnum.tmp_2)
    

    needs to be

    (void *)(&p2_tnum.tmp_2)
    

    And this is a local variable so you can’t return it.

    t_num p2_tnum
    

    Has to be a pointer with dynamic memory allocated.

    t_num *p2_tnum = (t_num *) malloc(sizeof(t_num));
    

    These fixes will require you to do some additional modifications, but I think you can manage those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> #include <pthread.h> #include <time.h> #include <stdlib.h> typedef struct pr_struct{ int owner; int
Here is the code #include<stdio.h> #include<sys/types.h> #include<stdlib.h> #include<pthread.h> typedef struct std_thread { char name[20];
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; void insert( struct
This is my code: #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> int num_mezzo_1(int
I have the following code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> #define NUM_THREADS 100
#include<stdlib.h> #include<string.h> #include<stdio.h> int pstrcmp( char **p,char **q) { return strcmp(*p,*q) ; } int
#include<stdio.h> #include<stdlib.h> char* re() { char *p = hello; return p; } int main()
#include <stdio.h> #include <stdlib.h> int main(void) { int x; int *in, *begin; in =
#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { if(argc != 2) return
#include <stdio.h> #include <stdlib.h> #include <time.h> void initDeck (int deck[]); void showDeck (int deck[]);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.