[英]Integer Casting Issue with Multi-threading in C
我正在嘗試編寫一個簡單的多線程程序,該程序將命令行參數(int)發送給函數,但是我無法正確轉換它。 以下是我程序的摘錄,不包括無關代碼。
int main(int argc, char **argv)
{
pthread_t thread1;
pthread_create(&thread1, NULL, simple_function, (void*)argv[1]);
pthread_join(thread1, NULL);
return EXIT_SUCCESS;
}
void* simple_function(void* num)
{
int n = *(int *)num;
printf(The number is: '%d', n);
n - 1;
...
}
我的程序實際做什么:
命令行輸入:14
預期的“ printf”結果:14
實際的'printf'結果:1476408369
轉換中缺少一些內容,但是我不確定該將其轉換為什么。 編譯過程中沒有警告,不勝感激。
最主要的是argv [1]實際上是(char *)而不是(int *)。 您需要使用atoi之類的將字符串轉換為數值。
void* simple_function(void* val)
{
char * c = (char *)val;
int n = atoi(c);
printf(The number is: '%d', n);
n - 1;
...
}
請注意,由於我目前無法使用C編譯器,因此我尚未實際測試上述代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.