2014-01-22 46 views
1

我目前正試圖從一本書中學習C++,這本書是我幾天前從我的一位朋友那裏得到的。我在書中看到了一些代碼作爲測驗,我需要解決。所以我試圖解決它們,但我不確定我的假設是否正確。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? 

我不要求你的答案,我需要有人來告訴我是否是正確的還是錯誤的,爲什麼請。

回答

2
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並給它分配一個值 '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值(字符)結尾的char數組(不是字符'0',其值爲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字符串,你需要的字符PLUS空間數之前返回字符數量用於終止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。

會是正確的 - 否則你會覆蓋分配給你的內存並導致損壞。

--- --- EDIT

投擲一些這一起,現場演示這裏:http://ideone.com/X8HPxP

的#include 的#include

INT主(){ 炭一個[] = 「你好」; std :: cout < <「a start as [」< < 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; 

}

+0

謝謝回答。所以你說第一個代碼沒有錯?我不應該使用char * ch ='B'而不是char ch ='B'對嗎?對於第二個,你怎麼知道你需要多少內存?我的意思是你怎麼知道要補充多少? – rullzing

+0

'char ch ='B''是正確的,因爲''B''是'char'。如果你想要一個char指針,你需要使用'&'運算符,就像下一行那樣('return &ch;')。如果你想使它成爲'char * b =',你需要做'char * b = &'B';'。 ''B'是一個字符,並且(在這個contxt中)'&'意味着'地址','char *'意味着'類型地址變量'。對於額外的內存大小,請參閱上面添加「---編輯---」的位置。 – kfsone

0

第一代碼:

R是具有返回類型char *即引用類型的函數名稱和接受的參考類型char * G參數。

'B'被分配給ch變量。

r函數返回ch變量的地址。

根據我在第一代碼中沒有要求更正。

第二碼:

是將在第2行導致編譯錯誤,因爲B不聲明或定義。

0
char* r(char *g){ 

這裏r()是採取char*作爲參數和返回char*

char ch = 'B'; 
return &ch; 

這裏功能,chchar本地定義的,你正在返回它。不是很好。更好地使用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來代替。

+0

「更好地使用char *。」 - 怎麼樣?該功能看起來不可修復。大概理由燒燬這本書。 – Potatoswatter

+0

@Patatoswatter它是堆棧中的char和變量,因此該變量的返回沒有多大意義。因此,我認爲char *會是更好的選擇。 –

0

一號代碼:

你要定義一個名爲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);