无法打印char! [英] Can't print char!

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

问题描述

我想打印字符''d'':


main(){


char g [4];

g [0] =''a'';

g [1] =''b'';

g [2] =''c '';

g [3] =''d'';


char * pp;

pp =& g [3];


printf("%s",* pp);

getchar();


}


但如果我改变的话,它唯一可以打印:


printf("%s",* pp) ;


进入:


printf("%s",pp);


但是当我用int做同样的事情时我必须使用第一个版本的

printf:


main(){


int j = 50;


int * q;


q =& j;


printf("%d",* q);

getchar();


}


为什么它与整数相反?


JS

解决案
与%s printf中打印一个字符串,由

第一个字符的一个指针来表示。它只是运气,它打印出一个字符,如果

以下地址的值为非零,它会打印出来

垃圾( - >一个字符串是null终止)。

要打印一个整数,你需要取消引用指针,因为printf

需要的是值,而不是地址。


simon


在文章< d1 ********** @ news.net.uni-c.dk> ;, JS< sf****@asdas.com>写道:

:我想打印字符''d'':


:char g [4];

:g [3] =''d'';


:pp =& g [3];


:printf(" ;%s",* pp);


* pp是单个字符,而不是字符串。您应该使用%c

格式进行打印 - 否则 - 'd''对应的值 -

将用作指向字符串打印,结果令人讨厌。


你也犯了一个错误,使用%s格式来处理

a字符数组,它不是NULL终止。

printf("%s",pp);将要从字符串的末尾开始运行

这是一件坏事。

-

如果你喜欢VT-52,那就太开心了的。


JS< sf **** @ asdas.com>这样说:

main(){


在C99中无效。 main()返回int。

char g [4];
g [0] =''a'';
g [1] =''b'';
g [2] =''c'';
g [3] =''d'';
char * pp;
pp =& g [3];


pp指向g的最后一个元素,即一个字符。

printf("%s",* pp);


你可能意味着


printf("%c \ nn",* pp);

的getchar();


在C89中不允许省略main()的退货声明。

printf("%s",pp);



非常糟糕。 %s格式说明符需要以null结尾的

字符数组,而pp则不需要。未定义的行为结果。


为什么它与整数相反?




因为你使用了不正确的格式说明符,如上所述。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


I would like to print char ''d'':

main(){

char g[4];
g[0] = ''a'';
g[1] = ''b'';
g[2] = ''c'';
g[3] = ''d'';

char *pp;
pp = &g[3];

printf("%s", *pp);
getchar();

}

But its only possible to get d printed if I change:

printf("%s", *pp);

into:

printf("%s", pp);

But when I do the same thing with an int I have to use the first version of
the printf:

main(){

int j = 50;

int *q;

q = &j;

printf("%d", *q);
getchar();

}

Why is it the other way around with integers??

JS

解决方案

with %s in printf you print a string, represented by a pointer of the
first char. it is just luck that it prints out one char, if the
following address would have a value of nonzero, it would print out
garbage (-> a string is null terminated).
to print an integer, you need to dereference the pointer, because printf
needs the value, not the address.

simon


In article <d1**********@news.net.uni-c.dk>, JS <sf****@asdas.com> wrote:
:I would like to print char ''d'':

: char g[4];
: g[3] = ''d'';

: pp = &g[3];

: printf("%s", *pp);

*pp is a single character, not a string. You should be using a %c
format to print it -- otherwise the -value- that ''d'' corresponds to
will be used as a pointer to the string to print, with nasty results.

You also are making a mistake by using a %s format to deal with
a character array that is not NULL terminated.
printf("%s", pp); is going to run off the end of the string
which is a Bad Thing.
--
Feep if you love VT-52''s.


JS <sf****@asdas.com> spoke thus:

main(){
Invalid in C99. main() returns int.
char g[4];
g[0] = ''a'';
g[1] = ''b'';
g[2] = ''c'';
g[3] = ''d''; char *pp;
pp = &g[3];
pp points to the last element of g, which is a character.
printf("%s", *pp);
You probably meant

printf( "%c\n", *pp );
getchar();
Omitting the return statement of main() is not permitted in C89.
printf("%s", pp);

Very bad. The %s format specifier requires a null-terminated
array of characters, which pp is not. Undefined behavior results.

Why is it the other way around with integers??



Because you used the incorrect format specifier, as above.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


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

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