繁体   English   中英

C ++调试中的指针

[英]pointers in c++ debugging

我目前正在尝试从几天前从我的一个朋友那里得到的书中学习C ++。 我在书中已经看到一些代码作为测验,需要解决。 所以我试图解决它们,但是我不确定我的假设是否正确。

这是第一个

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?

我不是在寻求答案,我需要有人告诉我是对还是错以及为什么要这样做。

char* r(char *g){ // can someone explain this line for me? I'm not sure what is it saying

声明一个带有一个参数的函数r ,一个指针g包含一个或多个字符的地址。

char ch = 'B';

声明一个char类型为ch的变量ch ,并为其分配值'B'。 也就是说,它将包含一个数字,该数字是字母B在ASCII图表中的位置。它将包含数字 66,但是当您打印出来时,它将生成字母“ B”。 (请参阅http://www.asciitable.com/

此变量可能会在堆栈上。 它可能在寄存器中,但是编译器通常很聪明,下一行将确保它在堆栈中。

return &ch;

在这种情况下, &是运算符的address of

return address_of(ch);

由于chchar类型的,因此&ch会产生char*类型的值。

char* a;

声明没有初始值的变量a 陷入写作习惯是一件坏事。

a = new char[strlen(b)];

您说b不存在,但我认为它假定为char*类型-指向一个或多个字符的指针。 在C和C ++中,“ C-String”是由'char'值(字符)组成的数组,该数组以值为0的char(不是字符'0',其ASCII值为48,但为0,或'\\ 0' )。 这称为“终止nul”或“ nul字符”或“ nul字节”。

字符串"hello"实际上可以表示为数组{ 'h', 'e', 'l', 'l', 'o', 0 } "hell0" ,后者为{ 'h', 'e', 'l', 'l', '0', 0 };

strlen函数从被调用的地址开始计算字符数,直到找到nul。 如果b是“ hello”的地址,则strlen将返回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返回字符数。

strcpy(a, b);

这将复制的字符指向b的地址在a ,*包括终止NUL。

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

char* a = new char[strlen(b) + 1];
strcpy(a, b);

同样,strlen总是要返回长度-字符数,而对于nul,您总是想要多于一个。

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

-编辑-

将其中一些内容放在一起,在此处进行实时演示: http : //ideone.com/X8HPxP

#include #include

int main(){char a [] =“ hello”; std :: cout <<“ a开头为[” << a <<“] \\ n”;

   // C/C++ arrays are 0-based, that is:
   a[0] = 'H'; // changes a to "Hello"

   std::cout << "a is now [" << a << "]\n";

   std::cout << "strlen(a) returns " << strlen(a) << "\n";

   // But that is based on counting characters until the 0.
   a[3] = 0; // one way to write it,
   a[3] = '\0'; // some people prefer writing it this way.

   std::cout << "a changed to [" << a << "]\n";

   std::cout << "strlen(a) is now " << strlen(a) << "\n";

   return 0;

}

第一个代码:

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

“ B”分配给ch变量。

r函数返回ch变量的地址。

据我说,不需要在第一代码中进行更正。

第二码:

是的,因为未声明或定义b,将在第2行导致编译错误。

char* r(char *g){ 

这里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未定义,请确保会出现错误。 如果bchar*并为其分配了一些值,则strlen将为您提供b具有的字符串的长度。 所以看起来不错

strcpy(a,b); // since we're using strcpy() a and b has to be pointers am I right?

是的,你是对的! 您可以改用strncpy。

第一个代码:

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

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); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM