什么是类型化指针(void *)p是什么意思? [英] what is typecasting a pointer to the type (void *)p mean?

查看:368
本文介绍了什么是类型化指针(void *)p是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我在大多数C程序中都看到,函数中的指针被接受为:
int func(int i,(void *)p)其中p是一个指针或地址,它是从调用它的地方传递的。
。你是什​​么意思指向

到一个虚空,为什么要这样做?

Aso,当我们将指针指向另一个类型时会发生什么。对于

例如int * i =(char *)p;

在不同情况下?我有点困惑。可以通过清楚地向我解释实际发生的事情来清楚这个混乱吗?

在这里?

再见

why do I see that in most C programs, pointers in functions are
accepted as:
int func(int i,(void *)p) where p is a pointer or an address which is
passed from the place where it is called. what do you mean by pointing
to a void and why is it done?
Aso, what happens when we typecast a pointer to another type. say for
example int *i=(char *)p;
under different situations? I am kind of confused..can anybody clear
this confusion by clearly explaining to me what actually is happening
out here?
Bye

推荐答案

2006年1月25日星期三12:17:23 -0800,Abhishek写道:
On Wed, 25 Jan 2006 12:17:23 -0800, Abhishek wrote:
int func(int i,(void *)p)其中p是指针或从调用它的地方传递的地址。你是什​​么意思指向
一个虚空,为什么要这样做?
指向void的指针是指向内存中不合格位置的指针。如果你想要使用线程
这是不可避免的。其中一个原因是将数据从一个进程传输到另一个进程的方式是
。你不能这样做

并同时保留类型信息。这也意味着

接收过程必须知道如何取消引用指针 - 将其转换为已知类型的




#include< stdio.h>

#include< pthread.h>

#include< unistd.h>


void test_p(void * t)

{

int i;

char * text =(char *)t;

for(i = 0; i< 10; ++ i)

printf("%s \ n",text);

pthread_exit(NULL);

}


int main()

{

pthread_t t1 = 0;

char * text =" test";


printf(" Howdy world!\ n");

pthread_create(& t1,NULL,(void *)& test_p,(void *)text);

pthread_join(t1,NULL);

返回0 ;

}

Aso,当我们将指针指向另一种类型时会发生什么。比如
例如int * i =(char *)p;
int func(int i,(void *)p) where p is a pointer or an address which is
passed from the place where it is called. what do you mean by pointing to
a void and why is it done? A pointer to a void is a pointer to an unqualified place in memory. If you
want to work with threads this is unavoidable. One of the reasons is a
way of transfering data from one process to another. You cannot do this
and at the same time preserve the type information. This also means that
the receiving process must know how to dereference the pointer - cast it
to a known type.

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void test_p(void *t)
{
int i;
char *text = (char *) t;
for (i = 0; i < 10; ++i)
printf("%s\n", text);
pthread_exit(NULL);
}

int main()
{
pthread_t t1 = 0;
char *text = "test";

printf("Howdy world!\n");
pthread_create(&t1, NULL, (void *)&test_p, (void *) text);
pthread_join(t1, NULL);
return 0;
}
Aso, what happens when we typecast a pointer to another type. say for example int *i=(char *)p;



这很危险,因为你可能会丢失信息,这取决于你的
编译器可能会发出警告或错误。

-

Hilsen /问候

Michael Rasmussen
http://keyserver.veridis.com:11371 / p ... rch = 0xE3E80917


This is dangerous since you could loose information an depending on your
compiler could course either a warning or an error.
--
Hilsen/Regards
Michael Rasmussen
http://keyserver.veridis.com:11371/p...rch=0xE3E80917




Abhishek写道:

Abhishek wrote:
为什么我看到在大多数C程序中,函数中的指针被接受为:
int func(int i,(void *)p)其中p是指针或从地方传递的地址在哪里被称为。你是什​​么意思指向一个虚空,为什么要这样做?


void指针是一个通用指针,意味着它可以用来存储任何数据类型的
地址。


by(void *)p您将p的地址转换为void *类型。

这实际上是多余的。在为void *指针赋值时,没有必要进行类型转换。

Aso,当我们将指针指向另一个类型时会发生什么。比如说
示例int * i =(char *)p;


这甚至不应该编译,因为你试图给int * i分配一个错误的

类型

的地址。

在不同的情况下?我有点困惑。可以通过清楚地向我解释实际发生的事情来清除这种混乱吗?
再见
why do I see that in most C programs, pointers in functions are
accepted as:
int func(int i,(void *)p) where p is a pointer or an address which is
passed from the place where it is called. what do you mean by pointing
to a void and why is it done?
void pointer is a generic pointer, meaning that it can be used to store
address of any data type.

by (void*)p you are casting the address of p to void* type.
This is actually redundant. Typecasting is not necessary
while assigning value to a void* pointer.
Aso, what happens when we typecast a pointer to another type. say for
example int *i=(char *)p;
This should not even compile because you''re trying to assign a wrong
type
of address to int *i.
under different situations? I am kind of confused..can anybody clear
this confusion by clearly explaining to me what actually is happening
out here?
Bye




Sharath AV



Sharath A.V


这个alsomean我有以下灵活性吗?


就像我可以使用test_p函数接受不同类型的

指针,通过类型化它们来输入void *然后执行一个动作

取决于我传递给函数的特定标志变量? br />
喜欢void test_p(void * t,int i)

{

int k;

if(i = = 1)

{

char * text =(char *)t;

for(k = 0; k <10; + + k)

printf("%s \ n",text);

}

else

{

int * j =(int *)t;

for(k = 0; k <10; k ++)

printf( %d \ nn,* j);

}


}


这里.. 。取决于''我'的价值我通过,我可以采取适当的

行动。但是,我知道如果我传递i = 1,那么我将指针传递给

字符串,通过类型转换为void *并且如果我通过i!= 2然后我传递一个

指向整数类型的指针将整数指针转换为void

* ???

请澄清


Michael Rasmussen写道:
Does this alsomean that I get the following flexibility?

like I can use the test_p function to accept different types of
pointers by typecasting them to type void * and later perform an action
depending on a particular flag variable I pass to the function?
like void test_p(void *t, int i)
{
int k;
if(i==1)
{
char *text = (char *) t;
for (k = 0; k < 10; ++k)
printf("%s\n", text);
}
else
{
int *j=(int *)t;
for(k=0;k<10;k++)
printf("%d\n",*j);
}

}

here...depending upon the value of ''i'' I pass, I can take appropriate
action. However, I know that if I pass i=1, then I pass a pointer to a
string by typecasting to void * and if I pass i !=2 then I pass a
pointer to an integer by type casting the integer pointer to type void
*???
Pls clarify

Michael Rasmussen wrote:
2006年1月25日星期三12:17:23 -0800,Abhishek写道:
On Wed, 25 Jan 2006 12:17:23 -0800, Abhishek wrote:
int func(int i,(void * )p)其中p是指针或从被调用的地方传递的地址。你是什​​么意思指向
一个空白,为什么要这样做?
int func(int i,(void *)p) where p is a pointer or an address which is
passed from the place where it is called. what do you mean by pointing to
a void and why is it done?


指向void的指针是指向内存中不合格位置的指针。如果你想要使用线程,这是不可避免的。其中一个原因是将数据从一个进程传输到另一个进程的方式。你不能这样做
同时保留类型信息。这也意味着接收过程必须知道如何取消引用指针 - 将其转换为已知类型。

#include< stdio.h>
#include< pthread.h>
#include< unistd.h>

void test_p(void * t)
{
int i;
char * text =(char *)t;
for(i = 0; i< 10; ++ i)
printf("%s \ n",text);
pthread_exit(NULL);
}
int main()
{
pthread_t t1 = 0;
char * text =" test" ;

printf(" Howdy world!\ n");
pthread_create(& t1,NULL,(void *)& test_p,(void *)text);
pthread_join(t1,NULL);
返回0;
}


A pointer to a void is a pointer to an unqualified place in memory. If you
want to work with threads this is unavoidable. One of the reasons is a
way of transfering data from one process to another. You cannot do this
and at the same time preserve the type information. This also means that
the receiving process must know how to dereference the pointer - cast it
to a known type.

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void test_p(void *t)
{
int i;
char *text = (char *) t;
for (i = 0; i < 10; ++i)
printf("%s\n", text);
pthread_exit(NULL);
}

int main()
{
pthread_t t1 = 0;
char *text = "test";

printf("Howdy world!\n");
pthread_create(&t1, NULL, (void *)&test_p, (void *) text);
pthread_join(t1, NULL);
return 0;
}

Aso,当我们将指针指向另一个类型时会发生什么。说
Aso, what happens when we typecast a pointer to another type. say


示例int * i =(char *)p;
example int *i=(char *)p;


这很危险,因为你可能会丢失信息,具体取决于你的<编辑器可能会发出警告或错误。
-
Hilsen / Regards
Michael Rasmussen
http://keyserver.veridis.com:11371/p...rch=0xE3E80917






这篇关于什么是类型化指针(void *)p是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆