2009-06-26 73 views
4
#include <cstring> 
int main() 
    { 
    char *pName = new char[10]; 
    char dummy[] = "dummy"; 
    strcpy(pName + 0,dummy);//how this is different from -->this works 
    strcpy(pName[0],dummy);//this one...--> error C2664: 'strcpy' : 
          //cannot convert parameter 1 
          //from 'char' to 'char *' 

    } 
+1

我想你的意思是strcpy(&(pName [0]),dummy); – 2009-06-26 12:07:42

回答

12
  • PNAME [0]是在字符陣列(一個字符)
  • PNAME是快捷方式& PNAME [0](的一個指針數組的第一個元件)
  • 第一元件

您得到您的錯誤的原因是因爲strcpy的需要一個指向一個字符(字符*),而不是一個char值(這是什麼PNAME [0])

+1

我認爲&pName [0]是pName的一個長形... – 2009-06-26 12:10:26

+0

所以[]運算符在指針上做了2件事情的解引用和添加? – yesraaj 2009-06-26 12:13:41

0

沒有區別。由於您尚未爲pName分配任何空間,因此它們都會崩潰。 :編輯:不再是一個崩潰 - 問題已編輯]

主要區別是一個文體,經常影響它適合周圍的代碼編寫的方式 - 主要是數組訪問或主要是指針訪問。

(編輯:假設你的真正用意& PNAME [0]布賴恩邦迪指出)

0

從技術上講,strcpy(pName[0], dummy);是不正確的。即使內存分配給它。

這是因爲pName[0]是'char'類型,而pName + 0是char *類型。他們都以不同的方式引用相同的記憶。

編譯器可以再打開strcpy(pName[0], dummy);strcpy((char*) pName[0], dummy);這是一種危險的隱式轉換。如果你的編譯器是像樣的,你會得到一個警告或錯誤(當你與你的「錯誤C2664」看到)。

0

陣列是簡單地自動地(通常)分配給存儲器的自動分配塊的指針。以你的例子,你可以聲明虛同樣爲:

char dummy[] = "dummy"; 
char *dummy = "dummy"; 

然後你就可以使用數組語法或指針語法來訪問數據:

char ch = dummy[0]; // get the first element of the array 
char ch = *dummy;  // get the data pointed to by dummy 

兩個[]*可以去重新使用-reference指針和數組,所以下面是等效的:

array[N]; 
*(ptr + N); 

鑑於第二種形式中,仍然​​是一個指針,只是˚F進一步沿陣列。這就是爲什麼你的例子在語法上是正確的。 ptr[N]是指針的去參考和爲char(在此上下文中)。

0

PNAME是指向新分配的內存。 char *pName = new char[10];

dummy也是一個數組/指針。 char dummy[] = "dummy";

pName是指針並指向基地址,即使您添加(pName + 0)仍然指向相同的內存位置,因爲您只能添加0。strcpy(pName + 0,dummy);

的strcpy使用指針變量,並在第一個參數的傳遞價值,因此你越來越錯誤strcpy(pName[0],dummy)

3

當使用C或C指針和數組處理++它確實有助於承認他們是非常明顯的結構(我認爲解釋這種區別的最好的書之一是一本叫做「Deep C Secrets」的書,如果我沒有記錯的話)。泥水是這樣一個事實:從數組名到指針有一種單向的無聲轉換(語言處理變量名稱時出現不一致) - 但是不要將這種衰減現象的存在解釋爲暗示等價。

爲了幫助我們有理由這一點,讓我們介紹一個「存儲單元」的想法。我們一個「存儲單元」模式具有兩個屬性:

a) value 
b) address 

然後,我們可以模擬一個簡單的C++爲具有兩個屬性變量(我們不抽象的這種低層次需要的類型):

c) name 
d) memory cell 

與大多數模型一樣,它有一些缺陷(不涉及具有多個元素的數組,但它對我們的目的來說足夠了)。

因此,例如:

// non-array variable: name 'i', and memory cell: value=3, address=0x0A 
int i = 3; 

// non-array variable: name 'p', and memory cell: value=0x0A, address=0x0B 
int *p = &i; 

// array variable: name 'a', and memory cell: vale=4, address=0x0C  
int a[1] = { 4 }; 

// non-array variable: name 'b', and memory cell: value=0x0C, address = 0x0D 
int (*b)[1] = &a; 

// non-array variable: name 's', and memory cell: value=0x0C, address = 0x0E 
int *s = &a[0]; 


// non-array variable: name 't', and memory cell: value=0x0C, address = 0x0F 
int *t = a; // Here is the key difference! read on... 

現在,這裏的一個數組變量和非陣列之間的主要區別(指針)C++變量:

當在C++中的變量名稱進行評價時,它總是評估其存儲單元的值,但有一個例外:如果該變量命名一個數組變量。
如果變量是它的計算結果爲所述存儲單元的地址陣列的名稱。
以上兩行值得再讀一遍。

下面是一些例子,以幫助澄清的影響(參照上述變量):

int k = i; // the 'i' name evaluates to the value of its cell, so 'k' is set to 3 

int *q = p; // 'p' evaluates to the value of its cell, so 'q' is set to 0x0A 

int *r = a; // 'a' evaluates to the *address* of its cell, so 'r' is set to 0x0C 

int (*c)[1] = b; // 'c' is set to 0x0D 

這絕不應意味着一個數組變量是相同作爲指針變量。
它們具有固有的不同類型,並且試圖將它們當作相同的(即,將變量名稱定義爲一個翻譯單元中的數組,並且作爲另一個翻譯單元中的指針)會導致不好的事情發生。

因此對於例如不要這樣做:

 
// myproj_file1.cpp 
int array[100] = { 0 }; // here 'array' evaluates to the *address* of the first memory cell 

// myproj_file2.cpp 
extern int* array; // here 'array' evaluates to the *value* of the first memory cell 
      // Assuming the linker links the two 
      // what it does if you read the assembly, is something like this: 
      // extern int* array = (int*) array[0]; 
      // but it doesn't have to, it can do anything, since the behavior is undefined 

我希望這有助於。 如果你還是覺得進一步澄清也許會有幫助,請詢問後續問題,不要猶豫,拿到「深度C的祕密」一書:)

複印件(庫) -
附:功能類型及其名稱和衰減與這篇文章的大部分無關
p.s.我也故意忽略當數組綁定到引用類型時不會發生數組到指針的轉換

相關問題