簡體   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