0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pthread_create()のような関数でポインタでなく整数を渡す

Last updated at Posted at 2023-07-12

(備忘録)(7/13 修正)

やりたいこと

pthread_createのような、void*型を引数にとる関数で整数を渡したい。

結論

void*型への互換性がある uintptr_t, intptr_t を使う。
整数 → uintptr_t (intptr_t) → void* のようにキャストすればよい。

    uint8_t hoge = 123;
    pthread_create(&thread, NULL, function, (void*)(uintptr_t)hoge);
    void *function(void* arg){
        uint8_t hoge = (uint8_t)(uintptr_t)arg
        return 0;
    }

型サイズには注意すること。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?