void *等同于char *? [英] Is a void * equivalent to a char *?

查看:118
本文介绍了void *等同于char *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码:


#include< stdio.h>


int main(无效)

{

char array [100] ="你最喜欢的车是什么?" ;;

void * vp =& array;


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


返回0;

}


printf()语句是否正确?或者是vp所需的char * *


printf("%s \ n",(char *)vp);


也就是说,假设void *指向一个字符串,打印字符串的正确方法是什么?

Given the following code:

#include <stdio.h>

int main(void)
{
char array[100] = "What is your favorite car?";
void *vp = &array;

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

return 0;
}

Is the printf() statement correct? Or is a cast to char * required for vp?

printf("%s\n", (char *) vp);

That is, assuming a void * is pointing to a string, what is the proper way
to print the string?

推荐答案

在文章< Xn ************************ @ 207.217.125.201>,

planetzoom < fo*@fox.orgwrote:
In article <Xn************************@207.217.125.201>,
planetzoom <fo*@fox.orgwrote:

>给出以下代码:
>Given the following code:


> #include< stdio.h>
>#include <stdio.h>


> int main(void)

char array [100] ="什么是你最喜欢的车?" ;;

void * vp =& array;

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

返回0;
}
>int main(void)
{
char array[100] = "What is your favorite car?";
void *vp = &array;
printf("%s\n", vp);
return 0;
}


> printf()语句是否正确?或者是vp所需的char * *
printf("%s \ n",(char *)vp);
也就是说,假设void *指向一个字符串,打印字符串的正确方法是什么?
>Is the printf() statement correct? Or is a cast to char * required for vp?
printf("%s\n", (char *) vp);
That is, assuming a void * is pointing to a string, what is the proper way
to print the string?



void *需要具有与char *相同的表示形式,因此

理论上上述应该有效。 (但它冒犯了我的感情;-)


-

编程就是在你忙于制定其他计划的时候发生的事情。

void* is required to have the same representation as char*, so
in theory the above should work. (But it offends my sensibilities ;-) )

--
Programming is what happens while you''re busy making other plans.


planetzoom写道:
planetzoom wrote:

给出以下代码:


#include< ; stdio.h>


int main(无效)

{

char array [100] ="你的是什么最喜欢的车?" ;;

void * vp =& array;


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


返回0;

}


printf()语句是否正确?或者是vp所需的char * *


printf("%s \ n",(char *)vp);
Given the following code:

#include <stdio.h>

int main(void)
{
char array[100] = "What is your favorite car?";
void *vp = &array;

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

return 0;
}

Is the printf() statement correct? Or is a cast to char * required for vp?

printf("%s\n", (char *) vp);



在这种特殊情况下可能实际上并不需要演员表,

但是......

The cast is probably not actually required in this special case,
but...


也就是说,假设void *指向一个字符串,打印字符串的正确方法是什么?

That is, assuming a void * is pointing to a string, what is the proper way
to print the string?



....通常最好不要依赖任何两种不同的指针类型

以相同的方式传递。将vp转换为char *,你肯定不会遇到问题。

....it''s generally best not to rely on any two different pointer types
being passed the same way. Convert vp to char *, and you''re sure not
to run into problems.


ro ****** @ ibd.nrc-cnrc.gc.ca (Walter Roberson)在

news:eq ********** @ canopus.cc.umanitoba.ca:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
news:eq**********@canopus.cc.umanitoba.ca:

文章< Xn * ***********************@207.217.125.201> ;,

planetzoom< fo*@fox.orgwrote:
In article <Xn************************@207.217.125.201>,
planetzoom <fo*@fox.orgwrote:

>>给出以下代码:
>>Given the following code:


>> #include< ; stdio.h中>
>>#include <stdio.h>


>> int main(void)
{char array [100] ="你最喜欢的是什么car?" ;;
void * vp =& array;
printf("%s \ n",vp);
返回0;
}
>>int main(void)
{
char array[100] = "What is your favorite car?";
void *vp = &array;
printf("%s\n", vp);
return 0;
}


>> printf()语句是否正确?或者是针对vp的char *需要
吗? printf("%s \ n",(char *)vp);
也就是说,假设void *指向一个字符串,打印字符串的正确方法是什么?
>>Is the printf() statement correct? Or is a cast to char * required
for vp? printf("%s\n", (char *) vp);
That is, assuming a void * is pointing to a string, what is the proper
way to print the string?



void *需要与char *具有相同的表示形式,因此

理论上应该有效。 (但它冒犯了我的感情;-)



void* is required to have the same representation as char*, so
in theory the above should work. (But it offends my sensibilities ;-)
)



好​​的,谢谢Walter和truedfx的解释。 :)

Ok, thank you Walter and truedfx for your explanations. :)


这篇关于void *等同于char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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