c+中的指针+;调试 我现在正试着从几天前从我朋友那里得到的一本书中学习C++。我在书中看到一些代码是我需要解决的测验。所以我试图解决这些问题,但我不确定我的假设是否正确

c+中的指针+;调试 我现在正试着从几天前从我朋友那里得到的一本书中学习C++。我在书中看到一些代码是我需要解决的测验。所以我试图解决这些问题,但我不确定我的假设是否正确,c++,pointers,C++,Pointers,这是第一个 char* r(char *g){ // can someone explain this line for me? I'm not sure what is it saying char ch = 'B'; // is the code going to be correct if I changed char ch to char* ch? return &ch; // since this is &ch, then the previous line shou

这是第一个

char* r(char *g){ // can someone explain this line for me? I'm not sure what is it saying
char ch = 'B'; // is the code going to be correct if I changed char ch to char* ch? 
return &ch; // since this is &ch, then the previous line should be char* ch, am I right?
  }
第二个代码:

    char* a;
    a = new char[strlen(b)]; // will this line cause a compiling error just because b is undefined ? since there is no length for b because it's not even there?
    strcpy(a,b); // since we're using strcpy() a and b has to be pointers am I right?
我不是在问答案,我需要有人告诉我我是对还是错以及原因。

第一个代码:

r是返回类型为char*的函数名,即引用类型和接受参数为引用类型char*g

“B”被分配给ch变量

r函数返回ch变量的地址

根据我的说法,第一个代码中不需要更正

第二个代码:

char* a;
a = new char[strlen(b)];
是,它将导致第2行的编译错误,因为b未声明或定义

char* r(char *g){ // can someone explain this line for me? I'm not sure what is it saying
声明一个函数,
r
,它接受一个参数,一个指针
g
,以包含一个或多个字符的地址

char ch = 'B';
strcpy(a, b);
char* a = new char[strlen(b) + 1];
strcpy(a, b);
声明一个
char
类型的变量
ch
,并为其赋值“B”。也就是说,它将包含一个数字,这是字母B在ASCII图表中的位置。它将包含数字66,但当您打印出来时,它将生成字母“B”。(见附件)

此变量可能位于堆栈上。它可以在寄存器中,但编译器通常是智能的,下一行将确保它在堆栈中

return &ch;
在此上下文中,
&
是操作员的
地址

return address_of(ch);
由于
ch
的类型为
char
&ch
产生一个
char*
类型的值

char* a;
声明一个没有初始值的变量
a
。进入写作习惯是件坏事

a = new char[strlen(b)];
您说
b
不存在,但我认为它被假定为
char*
类型—指向一个或多个字符的指针。在C和C++中,“C字符串”是由值0的字符(不是字符0)终止的“char”值(字符)数组,它的ASCII值为48,但“0”或“0”。这称为“终止nul”或“nul字符”或“nul字节”

字符串
“hello”
实际上可以表示为数组
{'h','e','l','l','o',0}
。与
“hell0”
相比,它将是
{'h','e','l','l','0',0}

函数
strlen
从调用它的地址开始计算字符数,直到找到nul为止。如果b是“你好”的地址,斯特伦将返回5

new为一个对象分配内存,或者在本例中为char类型的对象数组分配内存,其数量是strlen的返回值

size_t len = strlen(b);
char* a = new char[len];
在代码的这一点上,回想一下我关于终止nul的解释,strlen在找到0之前返回字符数。要存储C字符串,需要字符数加上终止NULL的空格

如果
b
是字符串“A”,则它由一个字符('A')和两个*char*s-'A',以及0组成。Strlen返回字符数

char ch = 'B';
strcpy(a, b);
char* a = new char[strlen(b) + 1];
strcpy(a, b);
这将把
b
指向的字符复制到
a
*的地址,包括终止nul

这里的错误是您只为字符分配了足够的内存

char ch = 'B';
strcpy(a, b);
char* a = new char[strlen(b) + 1];
strcpy(a, b);
再说一次,strlen总是会返回长度,即字符数,对于nul,您总是希望多出一个字符

正确-否则您将覆盖分配给您的内存并导致损坏

---编辑---

将部分内容放在一起,现场演示如下:

#包括 #包括

int main(){ 字符a[]=“你好”; 标准::cout 这里的
r()
函数将
char*
作为参数并返回
char*

char ch = 'B'; 
return &ch;
这里,
ch
char
本地定义的,您正在返回它。这不好。最好使用char*。char将只有一个字符,而如果使用char*,您可以有多个字符

char* ch = "Thats My string";
return ch; //Notice ch is a pointer. No need to use &
第二个代码:

char* a;
a = new char[strlen(b)];
如果
b
未定义,肯定会有错误。如果
b
char*
并为其分配了一些值,则
strlen
将为您提供
b
具有的字符串长度。因此,这看起来不错

strcpy(a,b); // since we're using strcpy() a and b has to be pointers am I right?
是的,你是对的!你可以用strncpy代替。

第一个代码:

您正在定义一个名为r的函数,该函数接受指向字符的指针并返回指向字符的指针

char* r(char *g){ 
    //stack char variable ch is initialized to B
    //changing char ch to char *ch will compile (with a warning) but then the address pointed by ch will contain garbage (value of 'B' projected as an address).
    char ch = 'B';
    //you are returning the address of ch which as seen above is a stack variable so you are causing undefined behavior. You should avoid this.
    return &ch; 
}
第二代码:

char* a;
// if b is undefined as you state then following line will cause compiling error. strlen() will calculate the length of the area at runtime so b must be at lease defined first.
a = new char[strlen(b)];  
//a is a pointer as you defined it above and points to the heap memory allocated by new
strcpy(a,b); 

“更好地使用char*”-怎么做?函数看起来不可编译。可能是烧掉这本书的原因。@potatosatter它是char和堆栈上的变量,所以返回该变量没有多大意义。因此,我认为char*将是更好的选择。感谢您的回答。您是说第一个代码没有问题吗?我不应该使用char*ch='B'而不是char ch='B'对吗?对于第二个,你怎么知道你需要多少内存?我的意思是你怎么知道要添加多少?
char ch='B'
是正确的,因为
'B'
是一个
char
。如果你想要一个指向char的指针,你需要使用
&
操作符,就像下一行那样(
return&ch;
)。如果您想让它成为
char*b=
某种东西,您需要执行
char*b=&b';
'b'
是一个字符,并且(在本节中)
&
表示“的地址”,而
char*
表示“类型为address-of-a-char的变量”。有关额外的内存大小,请参见上面我添加了“---EDIT--”的地方。