初始化指针 [英] Initialising a pointer

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

问题描述



我(当然)希望我知道这个功能是做什么的:


char * fun(void){

char * ptr =" Hello World";


返回ptr;

}


它返回一个指针存储在内存中某处的字符串是

只读。以下函数是否执行相同的操作并且

代码是否合法,也就是指针

初始化为指向某些只读字符串?


char * fun(void){

char * ptr;


ptr =" Hello World" ;


返回ptr;

}


我查看了常见问题解答,道歉是否是'在某个地方。 BTW,

Steve Summit感谢这个非常有用的资源。


Jesper


I (certainly) hope I know what this function does:

char *fun(void){
char *ptr = "Hello World";

return ptr;
}

It returns a pointer to a string stored somewhere in the memory and is
read-only. Does the following function do the same thing and is the
code legal at all, that is, is the pointer
initialised to point to some string that is read-only?

char *fun(void){
char *ptr;

ptr = "Hello World";

return ptr;
}

I have looked in the FAQ, apologising if it''s there somewhere. BTW,
Steve Summit thanks for this very useful resource.

Jesper

推荐答案

ma*********@bupkiss.net 写道:
ma*********@bupkiss.net wrote:

我(当然)希望我知道这个功能是做什么的:


char * fun(void){

char * ptr =" Hello World";


返回ptr;

}
I (certainly) hope I know what this function does:

char *fun(void){
char *ptr = "Hello World";

return ptr;
}



这样做会更加安全和清晰


const char * fun(void)

It would be much safer and clearer to make that

const char *fun(void)


它返回一个指向存储在内存中某个字符串的指针,并且是只读的
。以下函数是否执行相同的操作并且

代码是否合法,也就是指针

初始化为指向某些只读字符串?


char * fun(void){

char * ptr;


ptr =" Hello World" ;


返回ptr;

}
It returns a pointer to a string stored somewhere in the memory and is
read-only. Does the following function do the same thing and is the
code legal at all, that is, is the pointer
initialised to point to some string that is read-only?

char *fun(void){
char *ptr;

ptr = "Hello World";

return ptr;
}



是的,没有任何差异。


-

Ian Collins。

Yes, the there isn''t any difference.

--
Ian Collins.


ma ********* @ bupkiss.net 说:

>

我(当然)希望我知道这个功能是做什么的:


char * fun(void){

char * ptr =" Hello World";


返回ptr;

}


它返回一个指针存储在内存中某处的字符串是

只读。
>
I (certainly) hope I know what this function does:

char *fun(void){
char *ptr = "Hello World";

return ptr;
}

It returns a pointer to a string stored somewhere in the memory and is
read-only.



是的。改进:


const char * fun(void){

const char * ptr =" Hello World" ;;


返回ptr;

}


这会阻止来电者尝试修改字符串

函数返回指针。

Yes. An improvement:

const char *fun(void){
const char *ptr = "Hello World";

return ptr;
}

This will discourage callers from trying to modify the string to which
the function returns a pointer.


以下函数是否执行相同的操作并且

代码完全合法,即,是指针

初始化为指向一些只读的字符串?


char * fun(void){

char * ptr;


ptr =" Hello World";


return ptr;

}
Does the following function do the same thing and is the
code legal at all, that is, is the pointer
initialised to point to some string that is read-only?

char *fun(void){
char *ptr;

ptr = "Hello World";

return ptr;
}



是的,该函数做同样的事情,是的,代码是合法的,(是的,它是

能够在与前一个相同,并且

否,指针未初始化。初始化是在定义对象时向对象提供
的值。在你的例子中,这个

不会发生 - 相反,对象是/分配/

后面的语句中的值。但是,它仍然指向一个只读的

字符串。


-

Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。

Yes, that function does the same thing, yes, the code is legal, (yes, it
is capable of being improved in the same way as the previous one), and
no, the pointer is not initialised. Initialisation is the providing of
a value to an object at the time it is defined. In your example, this
doesn''t happen - rather, the object is /assigned/ a value in the
following statement. But yes, it still ends up pointing to a read-only
string.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


4月19日凌晨1点24分,Richard Heathfield< ; r ... @ see.sig.invalidwrote:
On Apr 19, 1:24 am, Richard Heathfield <r...@see.sig.invalidwrote:

mail1779 ... @ bupkiss.net说:
mail1779...@bupkiss.net said:

我(当然)希望我知道这个功能是做什么的:
I (certainly) hope I know what this function does:


char * fun(void){

char * ptr =" Hello World" ;;
char *fun(void){
char *ptr = "Hello World";


return ptr;

}
return ptr;
}


它返回一个指向存储在内存中某个字符串的指针,并且是只读的

It returns a pointer to a string stored somewhere in the memory and is
read-only.



我认为这将提供指向堆栈段的指针,因为ptr是一个

本地变量。指向堆栈段的指针并不是一个好主意

因为内容可能随时被覆盖(即使用下一个函数

调用)

I think this will deliver a pointer to the stack segment as ptr is a
local variable. Pointers to stack segment are not really a good idea
as the content may be overwritten at any time (i.e. with next function
call)


是的。改进:


const char * fun(void){

const char * ptr =" Hello World" ;;


返回ptr;


}


这会阻止来电者尝试修改
$ b的字符串$ b函数返回一个指针。
Yes. An improvement:

const char *fun(void){
const char *ptr = "Hello World";

return ptr;

}

This will discourage callers from trying to modify the string to which
the function returns a pointer.



但是不会改变指向的值可能随时被覆盖的事实,b / b
,因为内容在堆栈上。

您是否用gdb(或任何调试器)检查代码?

But does not change the fact that the value pointed to may be
overwritten at any moment, as the content lies on the stack.
Did you check the code with gdb (or any debugger?)


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

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