void *和int相互转换问题

znlyj 2011-03-28 11:33:04
pthread_create()中void *和int可以相互转换吗?
求解答
...全文
2103 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangguanya 2011-12-07
  • 打赏
  • 举报
回复
指针在 操作系统底层 都是 long型的, 这个很容易理解,
任何进程的进程空间是跟操作系统的位数相关的,32bit系统4G,64bit系统2的64次方,int不管在什么系统下都是32位,要想在64位系统中正常寻址64bit的空间,地址肯定也是 64位的了.

现在的很多程序不能正常移植到64位系统,就是 很多人以为 指针只是一个 32位数, 而事实上,int是32位,long是跟系统位数变化的,指针跟long一样.
hukui161 2011-03-30
  • 打赏
  • 举报
回复
强制转换。
  • 打赏
  • 举报
回复
也解决了我的问题@!
赵4老师 2011-03-29
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

void *thread_function(void *arg);

int main() {
int res;

pthread_t a_thread;
void *thread_result;

res = pthread_create(&a_thread, NULL, thread_function, (void *)12345);

if (res != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");

res = pthread_join(a_thread, &thread_result);

if (res != 0) {
perror("Thread join failed");
exit(EXIT_FAILURE);
}

printf("Thread joined, it returned %s\n", (char *)thread_result);

printf("Message is now %s\n", message);
exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {
printf("thread_function is running. Argument was %d\n", (int)arg);
sleep(3);
strcpy(message, "Bye!");
pthread_exit("Thank you for the CPU time");
}
赵4老师 2011-03-29
  • 打赏
  • 举报
回复
仅供参考
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

void *thread_function(void *arg);
char message[] = "Hello World";

int main() {
int res;

pthread_t a_thread;
void *thread_result;

res = pthread_create(&a_thread, NULL, thread_function, (void *)message);

if (res != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");

res = pthread_join(a_thread, &thread_result);

if (res != 0) {
perror("Thread join failed");
exit(EXIT_FAILURE);
}

printf("Thread joined, it returned %s\n", (char *)thread_result);

printf("Message is now %s\n", message);
exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {
printf("thread_function is running. Argument was %s\n", (char *)arg);
sleep(3);
strcpy(message, "Bye!");
pthread_exit("Thank you for the CPU time");
}
luciferisnotsatan 2011-03-29
  • 打赏
  • 举报
回复
可以强转
znlyj 2011-03-29
  • 打赏
  • 举报
回复
谢谢各位了,看pthread_create()这个函数有点迷糊了。。。
AnYidan 2011-03-29
  • 打赏
  • 举报
回复
可以
指针就是一个变量存储地址的编号,而在已知的计算机体系中,其内存空间的地址排列序号都是整型数据,其范围可以涵盖该计算机的最大内存寻址能力
pathuang68 2011-03-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hhh_hao 的回复:]

void *和int *????

转化是可以的, 指针是long型的
[/Quote]
严格意义上来说,指针是int类型的。但在32bit的机器上,long和int都是4bytes,所以1L兄如果要说是long的话,当然不能说错。

指针实际上就是一个32bit的int类型的数值而已。比如说:
(void *)(456789);
就把456789变成了指向地址456789的指针,即地址0x0006F855(456789的16进制表示形式),理论上,你可以读、写这个地址,实际上这样做是极端危险的。

上面是将int转换成指针的情况,下面这样就是把地址转换成int:
int a = 100;
cout << (int)(&a) << endl;
pathuang68 2011-03-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hhh_hao 的回复:]

void *和int *????

转化是可以的, 指针是long型的
[/Quote]
指针是int类型的。
某某9 2011-03-28
  • 打赏
  • 举报
回复
可以
(void *)&int
hhh_hao 2011-03-28
  • 打赏
  • 举报
回复
void *和int *????

转化是可以的, 指针是long型的

70,016

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧