int指向char [英] int pointing to char

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

问题描述

这段代码将一个int指针(明显)分配给一个char,

当我尝试通过

整数指针访问char的ascii值时(p),


我得到的是垃圾值或不知道我不知道!

''因为它显示差异的一致值。 chars,例如。

c =''a''ans => -9119


c =''b''ans => -9118


c =''c''ans => -9117


#包括< stdio.h>


void main()

{

char c =''a'';

int * p;


p =& c; / *可疑指针转换警告!* /

clrscr();

pf(" \ n%d",* p);

getch();

}


。 。 。 。什么事情发生了?


Gautam

this piece of code assigns an int pointer(evident) to a char,
and when i try to access the ascii value of the char through the
integer pointer(p) ,

what i get is a junk value or not i don''t know !
''cause it shows consistent values for diff. chars , for eg.
c = ''a'' ans=>-9119

c = ''b'' ans=>-9118

c = ''c'' ans=>-9117

... and so on
#include <stdio.h>

void main()
{
char c = ''a'';
int * p;

p = &c; /*suspicious pointer conversion WARNING !*/
clrscr();
pf("\n%d", *p);
getch();
}

. . . . whats happening ?

Gautam

推荐答案

2004-03-03,Gautam< ga ** ******@yahoo.com>写道:
On 2004-03-03, Gautam <ga********@yahoo.com> wrote:
这段代码将一个int指针(明显)分配给一个char,
当我尝试通过
整数指针访问char的ascii值时( p),

我得到的是垃圾价值与否我不知道!
因为它显示差异的一致值。 chars,例如。

c =''a''ans => -9119

c =''b''ans => -9118

c =''c''ans => -9117

......依此类推

#include< stdio.h>

void main()
{c / c''''';
int * p;

p =& c; / *可疑指针转换警告!* /
clrscr();
pf(" \ n%d",* p);
getch();
}


根据你的评论,你的编译器似乎正在完成它的工作,

并且警告你你做错了什么。这意味着

你应该修复它。更改c的类型,以便p可以指向它。


因为您似乎在学习C,所以您应该尝试坚持使用

标准语言特征。 pf(),clrscr()和getch()不是标准C语言的一部分。在托管环境中的标准C中,main

返回一个int。


#include< stdio.h>


int main(无效)

{

int c =''a'';

int * p;


p =& c;

printf("%d \ n",* p);

返回0;

}

。 。 。 。发生什么事了?
this piece of code assigns an int pointer(evident) to a char,
and when i try to access the ascii value of the char through the
integer pointer(p) ,

what i get is a junk value or not i don''t know !
''cause it shows consistent values for diff. chars , for eg.
c = ''a'' ans=>-9119

c = ''b'' ans=>-9118

c = ''c'' ans=>-9117

... and so on
#include <stdio.h>

void main()
{
char c = ''a'';
int * p;

p = &c; /*suspicious pointer conversion WARNING !*/
clrscr();
pf("\n%d", *p);
getch();
}
Your compiler seems to be doing its job, according to your comment,
and is warning you that you are doing something wrong. This means
you should fix it. Change the type of c so that p can point to it.

Since you appear to be learning C, you should try to stick to the
standard language features. pf(), clrscr() and getch() are not part of
the standard C language. In standard C in a hosted environment, main
returns an int.

#include <stdio.h>

int main(void)
{
int c = ''a'';
int * p;

p = &c;
printf("%d\n", *p);
return 0;
}
. . . . whats happening ?




你在向编译器说谎p指向的是什么。假设
你的程序中的pf()不是#defined为空语句,

你的程序会导致未定义的行为,所以任何事情都可能发生。


- James



You are lying to the compiler about what p is pointing to. Assuming
that pf() in your program is not #defined to be an empty statement,
your program results in undefined behavior, so anything could happen.

-- James


In< 44 ******************** *****@posting.google.com> ga********@yahoo.com (Gautam)写道:
In <44*************************@posting.google.com> ga********@yahoo.com (Gautam) writes:
这段代码为一个字符分配一个int指针(明显),


Nope,它不是。

和当我尝试通过
整数指针(p)访问char的ascii值时,


一个没有任何意义的操作。如果你想通过指针访问

char的值,你需要一个指向char的指针。没有其他指针

类型适合这项工作。

#include< stdio.h>

void main()


所以,在*发布之前你没有费心阅读常见问题解答*

{
char c =''a'';
int * p;

p =& c; / *可疑指针转换警告!* /


这实际上是一个致命的编程错误,只要你的程序是关于
。永远不要忽略编译器警告*之前*理解它的

意思。

clrscr();


这是什么废话?

pf(" \ n%d",* p);


什么是废话?

getch();


这是什么废话?

}

。 。 。 。发生什么事了?
this piece of code assigns an int pointer(evident) to a char,
Nope, it doesn''t.
and when i try to access the ascii value of the char through the
integer pointer(p) ,
An operation devoid of any sense. If you want to access the value of the
char through a pointer, you NEED a pointer to char. No other pointer
type is suitable for the job.
#include <stdio.h>

void main()
So, you didn''t bother to read the FAQ *before* posting...
{
char c = ''a'';
int * p;

p = &c; /*suspicious pointer conversion WARNING !*/
It''s actually a fatal programing error, as far as your program is
concerned. NEVER ignore a compiler warning *before* understanding its
meaning.
clrscr();
What''s this nonsense?
pf("\n%d", *p);
What''s this nonsense?
getch();
What''s this nonsense?
}

. . . . whats happening ?




你错误地定义了main(),错误地初始化指针,

错误地使用相同的指针并且调用了很多

的函数既不是你的程序也不是标准的C库定义的。你希望发生什么?$?

比较你的垃圾和正确的以下程序

使用了指出'a''的价值:


#include< stdio.h>


int main()< br $>
{

char c =''a'';

char * p =& c;


printf("%d \ n",* p);

返回0;

}


一种同样正确的方法是将c定义为int,将p定义为指向int的指针。


有时你想要一个指向unsigned char的指针指向
$ b另一种类型的对象的$ b地址,但是所有其他对象指针

类型应该指向相应类型的对象的地址。


只有专家才有权忽略这条规则,因为他们知道他们正在做什么。


Dan

- -

Dan Pop

DESY Zeuthen,RZ group

电子邮件: Da*****@ifh.de

< br>

Gautam写道:

这段代码为char指定一个int指针(明显),
当我尝试访问ascii值时char通过
整数指针(p),

我得到的是垃圾值或不知道我不知道!
''因为它显示了一致的值差异。 chars,例如。

c =''a''ans => -9119
c =''b''ans => -9118
c =' 'c''ans => -9117
......依此类推

#include< stdio.h>

void main()


未定义的行为。使用int main(void)

{
char c =''a'';
int * p;

p =和C; / *可疑指针转换警告!* /


嗯?为什么你没有做点什么呢?

clrscr();


没有这样的功能。

pf(" \ n%d",* p);


没有这样的功能。

getch();


没有这样的功能。

}

。 。 。 。发生了什么?

this piece of code assigns an int pointer(evident) to a char,
and when i try to access the ascii value of the char through the
integer pointer(p) ,

what i get is a junk value or not i don''t know !
''cause it shows consistent values for diff. chars , for eg.

c = ''a'' ans=>-9119
c = ''b'' ans=>-9118
c = ''c'' ans=>-9117
... and so on

#include <stdio.h>

void main()
Undefined behaviour. Use "int main(void)"
{
char c = ''a'';
int * p;

p = &c; /*suspicious pointer conversion WARNING !*/
Well? why didn''t you do something about it?
clrscr();
no such function.
pf("\n%d", *p);
no such function.
getch();
no such function.
}

. . . . whats happening ?




比较:


#include< stdio.h>

int main(void)

{

int c =''a'';

int * p;

p =& c;

printf("%d \ n",* p);

返回0;

}


并将结果值与您得到的值进行比较。这可能会给你一些关于你的机器发生了什么的线索,即使是所有未定义的行为也是如此。


-

Chuck F(cb********@yahoo.com)(cb********@worldnet.att.net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>使用worldnet地址!



Compare to:

#include <stdio.h>
int main(void)
{
int c = ''a'';
int *p;

p = &c;
printf("%d\n", *p);
return 0;
}

and compare the resultant values with those you got. That may
give you some clue as to what is going on in your machine even
with all the undefined behavior.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


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

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