帮助c指针 [英] Help in c pointers

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

问题描述



HI


int * p;

* p = 4;


char * msg;

* msg =''c'';


printf(" msg is%s",msg);

当我尝试传递值4来放置整数指针p指向

它说seg错误


但是对于char指针它工作正常。


为什么会出现这种情况


帮助


HI

int *p;
*p = 4;

char * msg;
*msg = ''c'';

printf("msg is %s",msg);
when i try to pass a value 4 to place what integer pointer p points to
it says seg fault

but for the char pointer it works fine.

Why this behaviour

Help

推荐答案


ma *********** @ gmail.com schrieb:

ma***********@gmail.com schrieb:
int * p;
* p = 4;
int *p;
*p = 4;




使用指针时你已经完成了greates编程错误。

当它指向某个地方时不要使用指针!


最好试试这个:


int * p;


p =(int *)malloc(sizeof(int));


* p = 4;



You have done the greates programming fault when using pointers.
Never use a pointer when it points somewhere!

Better try this:

int * p;

p = (int *) malloc (sizeof(int));

*p = 4;





Thanx快速回复。


在这里我担心为什么在使用char *时我能够传递价值

给它。即使这指向某个地方也是正确的。

所以当使用int *指向somwhere我无法传递价值

但它使用char *我能够做到这一点。为什么???

提前预付



Thanx for the quick reply.

In here i am concerned why while using char * i am able to pass value
to it. Even this points to somewhere right.
So while using int * which points somwhere i am not able to pass value
to it but while using the char * i am able to do it . Why???
Thanx in advance


" Zero" < CH ******** @ web.de>在新闻中写道:1141121017.828706.126940

@ e56g2000cwe.googlegroups.com:
"Zero" <ch********@web.de> wrote in news:1141121017.828706.126940
@e56g2000cwe.googlegroups.com:
ma *********** @ gmail.com schrieb:
ma***********@gmail.com schrieb:
int * p;
* p = 4;
使用指针时你已经完成了greates编程错误。
当它指向某个地方时不要使用指针!
int *p;
*p = 4;
You have done the greates programming fault when using pointers.
Never use a pointer when it points somewhere!




ITYM:如果它没有*指向任何地方,就不要使用指针(也就是说,

从不使用未初始化的指针。

最好试试这个:

int * p;

p =(int *)malloc(sizeof(int));

* p = 4;



ITYM: Never use a pointer if it does *not* point to anywhere (that is,
never use an uninitialized pointer.
Better try this:

int * p;

p = (int *) malloc (sizeof(int));

*p = 4;




最好不要。没有必要强制转换malloc的返回值,并且这样做可以隐藏错误。总是,是*总是*,检查malloc

成功:


int * p = malloc(sizeof * p);

if(p){

* p = 4;

}否则{

/ *句柄错误* /

}


思南

-

A. Sinan Unur< 1u ** @ llenroc.ude.invalid>

(反转每个组件并删除.invalid for email address)


comp.lang.perl.misc关于WWW的指南:
http://mail.augustmail.com/~tadmc/cl...uidelines.html



Better not. There is no need to cast the return value of malloc, and
doing so can hide errors. Always, yes *always*, check that malloc
succeeded:

int *p = malloc(sizeof *p);
if ( p ) {
*p = 4;
} else {
/* handle error */
}

Sinan
--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html


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

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