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 6943877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:12:48+00:00 2026-05-27T13:12:48+00:00

While working with Threads in C, I’m facing the warning warning: cast to pointer

  • 0

While working with Threads in C, I’m facing the warning

“warning: cast to pointer from integer of different size”

The code is as follows

#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<pthread.h>
void *print(void *id)
{
 int a=10;
 printf("My thread id is %ld\n",pthread_self());
 printf("Thread %d is executing\n",id);
 return (void *) 42;
}

int main()
{
 pthread_t th[5];
 int t;
 int i;
 int status;
 void *ret;
 for(i=0;i<5;i++)
 {
   status=pthread_create(&th[i],NULL,print,(void *)i); //Getting warning at this line
   if(status)
   {
    printf("Error creating threads\n");
    exit(0);
   }
   pthread_join(th[i],&ret);
   printf("--->%d\n",(int *)ret);
 }
 pthread_exit(NULL);
}

Can anybody explain how to pass an integer to a function which receives (void * ) as a parameter?

  • 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-05-27T13:12:49+00:00Added an answer on May 27, 2026 at 1:12 pm

    This is a fine way to pass integers to new pthreads, if that is what you need. You just need to suppress the warning, and this will do it:

    #include <stdint.h>
    
    void *threadfunc(void *param)
    {
        int id = (intptr_t) param;
        ...
    }
    
    int i, r;
    r = pthread_create(&thread, NULL, threadfunc, (void *) (intptr_t) i);
    

    Discussion

    This may offend your sensibilities, but it’s very short and has no race conditions (as you’d have if you used &i). No sense in writing a few dozen lines of extra code just to get a bunch of numbered threads.

    Data races

    Here is a bad version with a data race:

    #include <pthread.h>
    #include <stdio.h>
    
    #define N 10
    
    void *thread_func(void *arg)
    {
        int *ptr = arg;
        // Has *ptr changed by the time we get here?  Maybe!
        printf("Arg = %d\n", *ptr);
        return NULL;
    }
    
    int main()
    {
        int i;
        pthread_t threads[N];
        for (i = 0; i < N; i++) {
            // NO NO NO NO this is bad!
            pthread_create(&threads[i], NULL, thread_func, &i);
        }
        for (i = 0; i < N; i++) {
            pthread_join(threads[i], NULL);
        }
        return 0;
    }
    

    Now, what happens when I run it with the thread sanitizer?

    (Also, check out how it prints “5” twice…)

    ==================
    WARNING: ThreadSanitizer: data race (pid=20494)
      Read of size 4 at 0x7ffc95a834ec by thread T1:
        #0 thread_func /home/depp/test.c:9 (a.out+0x000000000a8c)
        #1 <null> <null> (libtsan.so.0+0x000000023519)
    
      Previous write of size 4 at 0x7ffc95a834ec by main thread:
        #0 main /home/depp/test.c:17 (a.out+0x000000000b3a)
    
      Location is stack of main thread.
    
      Thread T1 (tid=20496, running) created by main thread at:
        #0 pthread_create <null> (libtsan.so.0+0x0000000273d4)
        #1 main /home/depp/test.c:18 (a.out+0x000000000b1c)
    
    SUMMARY: ThreadSanitizer: data race /home/depp/test.c:9 thread_func
    ==================
    Arg = 1
    Arg = 2
    Arg = 3
    Arg = 4
    Arg = 5
    Arg = 6
    Arg = 7
    Arg = 8
    Arg = 9
    Arg = 5
    ThreadSanitizer: reported 1 warnings
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read in here I should stick with STA threads while working inside ArcMap.
I am using the code from this website (http://www.helloandroid.com/tutorials/using-threads-and-progressdialog) in my App. It is
Actually I am working on real-time application with multiple threads. While I am running
While working in a Java app, I recently needed to assemble a comma-delimited list
While working between a Windows MySQL server and a Debian MySQL server, I noticed
While working on a project, I came across a JS-script created by a former
While working on a C++ project, I was looking for a third party library
While working on an existing project I suddenly got the following error when trying
While working a project tonight, I ended up using one .js resource file for
While working my way through the Android tutorials, I came across something I don't

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.