字符串指向字符串 [英] character pointer to string

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

问题描述

我知道什么是指针,但我总是通过字符串指向字符串模糊。你能告诉我这些吗?谢谢。


有什么区别?

char string [] =" abc" ;; //在这之间和

char * string =" abc";


为什么?

这一项工作

展开 | 选择 | Wrap | 行号

解决方案

让我们从这里开始:


char string [] =" abc" ;; //在这之间和

char * string =" abc" ;;



第一个是包含abc和null终止符的4个字符的本地数组。

第二个是指向const数组的指针cotaining abc和null终止符。


你可以对本地数组进行更改,但是你会在尝试更改文字内容时崩溃。


请记住,在C和C ++中,数组的名称是元素0的地址。

因此,在这两种情况下,名称 string 是元素0的地址数组。因此,在两种情况下,名称 string 都是char *。


const char * get()

{

返回" xyz" ;;

}



这样做是因为你要回来了x的地址。文字保存在静态内存池中,并在函数完成后存在。


const char * get()

{

string str =" xyz";

return str.c_str();

}


这是失败的,因为str是一个局部变量。它保存在一个区域中,称为堆栈帧。函数的堆栈框架包含传入的所有函数参数以及所有局部变量和其他一些东西。在调用函数时创建堆栈帧,并在函数完成时销毁。考虑到这一点,在这里你返回一个指向x的char *。不幸的是,当函数完成时,堆栈帧被销毁,字符串str返回到空闲内存。那些奇怪的角色是你做显示时内存中的任何东西。


这是一个特别糟糕的错误。你知道,记忆并没有被摧毁。它只是被重用了。这意味着返回一个指向局部变量的指针实际上可以工作,直到该内存被重用。一次该程序工作,然后下一次它没有。硬规则是永远不会返回指针或对局部变量的引用。



这样做是因为你要返回的地址X。文字保存在静态内存池中,并在函数完成后存在。



谢谢你的解释非常有帮助。如果返回x ONLY的地址,为什么还会打印y和z? y和z的地址是否与x相同?



感谢解释非常有帮助。如果返回x ONLY的地址,为什么还会打印y和z? y和z的地址是否与x相同?



因为它是一个数组。您只需返回第一个元素的地址,该数组就可以自动访问其余元素,因为它们位于内存中的第一个元素之后。就像说的那样,数组的名称实际上是指向第一个元素的指针,因此它与它相同。


I know what is pointer, but I always blurred by character pointer to string. Can you tell me these accordingly ? Thanks.

What is the difference ?
char string[] = "abc"; // between this one and
char * string = "abc";

How come ?
This one work

Expand|Select|Wrap|Line Numbers

解决方案

Let''s start here:

char string[] = "abc"; // between this one and
char * string = "abc";

The first is a local array of of 4 char containing abc and a null terminator.
The second is a pointer to a const array cotaining abc and a null terminator.

You canmake changes to the local array but you will crash trying to change the contents of the literal.

Remember, in C and C++, the name of the array is the address of element 0.
So, in both cases, the name string is the address of element 0 of the array. Therefore, the name string is a char* in both cases.

const char * get()
{
return "xyz";
}

This works because you are returning the address of the x. The literal is kept in the static memory pool and will exist after the function has completed.

const char * get()
{
string str = "xyz";
return str.c_str();
}

This fails because str is a local variable. It is kept in an area call a stack frame. The stack frame for a function contains all of the function arguments that were passed in plus all of the local variables and some other stuff. The stack frame is created when the function is called and destroyed when the function completes. With that in mind, here you are returning a char* that points to the x. Unfortunately, when the function completes, the stack frame is destroyed and the string str is returned to free memory. Those strange characters are whatever is in memory at the time you did the display.

This is a particularly bad bug. You see, memory isn''t destroyed. It''s just reused. That means returning a pointer to a local variable may actually work until that memory is reused. One time the program works, then next time it doesn''t. The hard rule is to never return a pointer or a reference to a local variable.


This works because you are returning the address of the x. The literal is kept in the static memory pool and will exist after the function has completed.

Thanks the explanation is very helpful. if the address of the x ONLY is returned why does the y and z get printed as well ? is the address of the y and z same as the x ?


Thanks the explanation is very helpful. if the address of the x ONLY is returned why does the y and z get printed as well ? is the address of the y and z same as the x ?

Because it''s an array. You only need to return the address of the first element, the array can automatically access the rest of the elements because they come right after the first element in memory. Like was said, the name of an array is actually a pointer to the first element, so it''s the same as that.


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

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