指针int / char ... [英] pointer int/char ...

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

问题描述



在声明/定义指向int / char的指针时,究竟是什么做了

编译器。我能够获得所有指向

char定义的指针的输出(字符串)(不包括第8行和第14行)。但当我包括#8&

第14行时,其唯一的印刷生产线10& 11&立刻给它了一下

的错误。请给出建议,了解内部事物abt

声明/指针变量的定义。


1 #include< stdio.h>

2

3 int main()

4 {

5 char * a =" linux";

6 char * b =" linux";

7 char * c =" linux";

8 int * i = 4;

9

10 printf(" \\\ =%s",a);

11 printf(" \ nb =%s",b );

12 printf(" \ nc =%s",c);

13

14 printf(" \ ni =%d",* i);

15

16返回0;

17}


注意:

我只是在做指向

interger声明的时候得到一些奇怪的结果。 (有时候我可以分配/打印指针

整数变量值即使没有分配内存)

操作系统:Linux 2.4.20-8 i686 i686 i386 GNU / Linux

编译器:gcc版本3.2.2 20030222(Red Hat Linux 3.2.2-5)


ThankX in Advance .......


Urs

Saravanan .....

解决方案

bvb< bm******@fastem.com>潦草地写道:


在声明/定义指向int / char的指针时,究竟是什么做了
编译器。我能够获得所有指向
字符定义的指针的输出(字符串)(不包括第8行和第14行)。但是,当我包括第8行&#
第14行时,其唯一的印刷生产线10& 11&立即给出了故障。请给出建议,以了解内部事物abt
声明/定义指针变量。


您导致未定义的行为,首先通过为指针分配一个整数

值,然后通过该指针间接指向。你的

实现可能通过尝试访问虚拟内存表中的地址4

来实现这一点,这很可能是在内存中的一部分

这不属于您的过程,因此导致

分段错误。

您正在尝试做什么?也许你想要这个:

int i = 4;

int * ip =& i;

1 #include< stdio.h>
2
3 int main()
4 {
5 char * a =" linux";
6 char * b =" linux";
7 char * c =" linux";
8 int * i = 4;
9
10 printf(" \\\
a =%s",a);
11 printf(" \ nb =%s",b);
12 printf(" \ nc =%s",c);
13
14 printf( " \ n =%d",* i);
15
16返回0;
17}
注意:
我也得到一些只做指向
interger声明的奇怪结果。 (有时候我可以分配/打印指针来交换变量值,即使没有ALLOCATIG MEMORY)


这只是运气不好。未定义的行为意味着任何事情都可能发生:你的程序甚至可能正常工作。

操作系统:Linux 2.4.20-8 i686 i686 i386 GNU / Linux
编译器:gcc版本3.2.2 20030222(Red Hat Linux 3.2.2-5)


这与comp.lang.c无关。

ThankX提前.......
Urs
Saravanan .....




我不知道你是熊。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------- -----芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------------------规则! -------- /

即使在我的幻想中,每个人都是混蛋?

- Daria Morgendorfer


在< d5 ************************** @ posting.google.com> bm******@fastem.com (bvb)写道:


在声明/定义指向int / char的指针时,究竟是什么做了
编译器。我能够获得所有指向
字符定义的指针的输出(字符串)(不包括第8行和第14行)。但是,当我包括第8行&#
第14行时,其唯一的印刷生产线10& 11&立即给出了故障。请给出建议,了解指针变量的内部事项。

1#include< stdio.h>
2
3 int main()
4 {
5 char * a =" linux";
6 char * b =" linux";
7 char * c =" linux";
8 int * i = 4;


您的编译器是否默默接受上述行?!?我不敢相信

那个!

9
10 printf(" \\\
a =%s",a);
11 printf(" \ nbb =%s",b);
12 printf(" \ nc =%s",c);
13
14 printf(" \\ \\ _ =%d",* i);
15
16返回0;
17}

注意:
我也得到了只做指向
interger声明的一些奇怪的结果。 (有时候我可以分配/打印指针来交换变量值,即使没有ALLOCATIG内存)
操作系统:Linux 2.4.20-8 i686 i686 i386 GNU / Linux
编译器:gcc版本3.2.2 20030222(Red Hat Linux 3.2.2-5)




然后,您的编译器发出以下诊断:


test.c:8:警告:初始化使得指针来自整数而没有演员表


你完全得到了你应该忽略的东西。如果编译器是

告诉你你的代码是不正确的,那么执行它的重点是什么,

无论如何都没有修复它?


无论如何,FAQ解释了如何正确初始化指针。


Dan

-

Dan流行

DESY Zeuthen,RZ集团

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


In< cc ********** @ oravannahka.helsinki.fi> Joona I Palaste< pa ***** @ cc.helsinki.fi>写道:

bvb< bm ****** @ fastem.com>潦草地写道:

OS:Linux 2.4.20-8 i686 i686 i386 GNU / Linux
编译器:gcc版本3.2.2 20030222(Red Hat Linux 3.2.2- 5)



这与comp.lang.c无关。




当OP没有关于源的线索时对于这个问题,提及这些细节是一个好的*

的想法。据他所知,代码可能是正确的

但是在实现中遇到了一个错误。我们在过去看到过这样的案例

,与微软编译器中的错误有关。


劝阻人们提供这样的纯粹是愚蠢的>
信息。


Dan

-

Dan Pop

DESY Zeuthen ,RZ集团

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


Hi,
while declaring/defining pointer to int/char, What is doing the
compiler exactly . i''m able to get output(String) for all pointer to
char definition(without line # 8&14). But when i include line #8 &
line #14 Its only printing lines 10 & 11 & immediatly its giving seg
fault. Please give suggestion to understand the internal things abt
declaration/definition of pointer varibles.

1#include<stdio.h>
2
3 int main()
4 {
5 char *a="linux";
6 char *b="linux";
7 char *c="linux";
8 int *i=4;
9
10 printf("\n a = %s",a);
11 printf("\n b = %s",b);
12 printf("\n c = %s",c);
13
14 printf("\n i=%d",*i);
15
16 return 0;
17 }

NOTE:
Also i''m getting some strange result when just doing pointer to
interger declaration. (Sometimes i''m able to assign/print pointer to
interger variable values EVEN WITHOUT ALLOCATIG MEMORY)
OS : Linux 2.4.20-8 i686 i686 i386 GNU/Linux
Compiler : gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

ThankX in Advance.......

Urs
Saravanan.....

解决方案

bvb <bm******@fastem.com> scribbled the following:

Hi,
while declaring/defining pointer to int/char, What is doing the
compiler exactly . i''m able to get output(String) for all pointer to
char definition(without line # 8&14). But when i include line #8 &
line #14 Its only printing lines 10 & 11 & immediatly its giving seg
fault. Please give suggestion to understand the internal things abt
declaration/definition of pointer varibles.
You are causing undefined behaviour, first by assigning an integer
value to a pointer, then by indirecting through that pointer. Your
implementation probably implements this by trying to access address 4
in the virtual memory table, which very likely is in a part of memory
that does not belong to your processs, therefore causing a
segmentation fault.
What is it exactly that you are trying to do? Perhaps you want this:
int i = 4;
int *ip = &i;
1#include<stdio.h>
2
3 int main()
4 {
5 char *a="linux";
6 char *b="linux";
7 char *c="linux";
8 int *i=4;
9
10 printf("\n a = %s",a);
11 printf("\n b = %s",b);
12 printf("\n c = %s",c);
13
14 printf("\n i=%d",*i);
15
16 return 0;
17 } NOTE:
Also i''m getting some strange result when just doing pointer to
interger declaration. (Sometimes i''m able to assign/print pointer to
interger variable values EVEN WITHOUT ALLOCATIG MEMORY)
This is just sheer dumb luck. Undefined behaviour means that anything
can happen: your program might even appear to work.
OS : Linux 2.4.20-8 i686 i686 i386 GNU/Linux
Compiler : gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
This is not relevant to comp.lang.c.
ThankX in Advance....... Urs
Saravanan.....



I didn''t know you were a bear.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"How come even in my fantasies everyone is a jerk?"
- Daria Morgendorfer


In <d5**************************@posting.google.com > bm******@fastem.com (bvb) writes:

Hi,
while declaring/defining pointer to int/char, What is doing the
compiler exactly . i''m able to get output(String) for all pointer to
char definition(without line # 8&14). But when i include line #8 &
line #14 Its only printing lines 10 & 11 & immediatly its giving seg
fault. Please give suggestion to understand the internal things abt
declaration/definition of pointer varibles.

1#include<stdio.h>
2
3 int main()
4 {
5 char *a="linux";
6 char *b="linux";
7 char *c="linux";
8 int *i=4;
Does your compiler silently accept the above line?!? I can''t believe
that!
9
10 printf("\n a = %s",a);
11 printf("\n b = %s",b);
12 printf("\n c = %s",c);
13
14 printf("\n i=%d",*i);
15
16 return 0;
17 }

NOTE:
Also i''m getting some strange result when just doing pointer to
interger declaration. (Sometimes i''m able to assign/print pointer to
interger variable values EVEN WITHOUT ALLOCATIG MEMORY)
OS : Linux 2.4.20-8 i686 i686 i386 GNU/Linux
Compiler : gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)



Then, your compiler is issuing the following diagnostic:

test.c:8: warning: initialization makes pointer from integer without a cast

You got exactly what you deserved for ignoring it. If the compiler is
telling you that your code is incorrect, what''s the point in executing it,
anyway, without fixing it?

Anyway, the FAQ is explaining how to initialise pointers properly.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de


In <cc**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:

bvb <bm******@fastem.com> scribbled the following:

OS : Linux 2.4.20-8 i686 i686 i386 GNU/Linux
Compiler : gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)



This is not relevant to comp.lang.c.



When the OP has no clue about the source of the problem, it is a *good*
idea to mention such details. For all he knows, the code might be correct
but is hitting a bug in the implementation. And we have seen such cases
in the past, related to bugs in Microsoft compilers.

It is sheer stupidity to discourage people from providing such
information.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de


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

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