簡體   English   中英

理解char數組[]和字符串

[英]Understanding char array[] and string

我是編程新手。 我正在學習C作為我的第一門編程語言。 我發現一些奇怪的東西要理解。

我已經了解到在C中我們可以將String表示為像這樣的字符序列(使用char數組):

char status[10] = "Married";   

我了解到這種方法的問題是我們必須在編譯期間告訴status數組的大小。

但現在我已經學會了,我們可以用一個char指針來表示一個string喜歡-

char status[10] = "Married";
char *strPtr;
strPtr = status;

我不理解它。 我的問題是 -

  1. 如何使用strPtr在索引4(即Married )獲取char?

  2. status ,在char數組表示的string末尾有一個空字符( \\0 ) - M - a - r - r - i - e - d - \\0 因此,通過使用空字符( \\0 ),我們可以理解字符串的結尾。 當我們使用strPtr ,我們如何理解string

char *strPtr;
strPtr = status;

現在你的指針strPtr指向數組中的第一個字符,你可以這樣做

int i =0;
while( strPtr[i] != '\0')
{
  printf("%c ",strPtr[i]);
  i++;
}

*strPtr被稱為取消引用指針以獲取存儲在指針指向的位置的值。

記下來

strPtr[4] = *(strPtr +4); 

兩者都將獲得存儲在數組索引4處的值。

注意指針和數組名稱之間的區別:

----------------------------------
| s  | t  | r  | i  | n | g | \0 |
----------------------------------
  |
strPtr
status

strPtr ++將使您的指針指向數組中的下一個元素。

| s  | t  | r  | i  | n | g | \0 |
----------------------------------
       |
      strPtr

而您不能為陣列名稱執行此操作

不允許使用status++因為數組不是可修改的左值

很高興知道:

char status[10] = "Married";

只是等價的語法糖

char status[10]; // allocate 10 Bytes on stack
status[0] = 'M';
status[1] = 'a';
...
status[6]= 'd';
status[7] = '\0'; // same as 0

沒有更多,沒有更少。

也:

char c = status[3];

與... 完全相同

char c = *(status+3);

表達status[10]僅僅是*(status+10)語法糖。

在引擎蓋下使用\\0終止來檢查結束,如果你自己實現了一些字符串處理程序,你也可以這樣做,或者你可以忽略它並使用字符串給出的其他參數size ,或者你可以(不要!)選擇其他任何東西作為終止符號。

這不僅僅是char數組或'字符串',C數組只是指向類似類型的連續塊的指針,編譯時檢查你的'數組'下標不會超出'在聲明時指定的“結束”。 使用*(array+offset)表示法,您需要自己檢查。

要在索引4 strPtr處獲取字符,只需使用strPtr[4] (這也適用於status )。

要在使用strPtr時獲取字符串的strPtr ,您需要遍歷字符並查找終止\\0 這就是printf("%s", strPtr)在打印字符串時所做的事情(當它解析"%s"表達式時,它也只是另一個字符串)。 要在C中查找字符串中的許多有效字符,請使用strlen()函數。 哦,並確保你不做這樣的事情:

char a[3];
strcpy(a, "Hello!");

因為這會將7個字節寫入3字節的內存空間,因此會覆蓋您不想覆蓋的內容。

字符串末尾的'\\ 0'是一個無用的附加組件,旨在簡化或安全。 您可以使用'sizeof'來判斷字符串的最后一個字符,如下所示:

char status[] = "Married"; 

size_t szLastCharstatus = sizeof(status) / sizeof(status[0]) - 2;

char chLastChar = status[szLastCharstatus];

詳細說明:

sizeof(status)

返回數組occpuies的字節數。

sizeof(status[0])

返回第一個元素占用的字節數(以及其余的)。

這兩個值之間的區分給出了數組中元素的數量。 要訪問最后一個元素,我們需要減去一次,因為數組中的元素從零開始計數,因為字符串中的最后一個字符是'\\ 0'。

另請注意,數組不是指針,反之亦然。 數組有一個隱式轉換為第一個元素的指針,常量大小和它們自己的類型。 它們可以通過指針或值傳遞(使用結構hack是第二個)。

請注意,我使用' size_t ',它是存儲一些大小數據的變量的類型def。

我要做一個挑釁性的陳述:想到這個的方法是C沒有字符串 C只有char數組。 盡管它的名字, char實際上是一個數字類型( 'A' ,例如,只是一個有趣的方式來寫一個數字,通常是65)。

char數組與int數組或任何其他數值類型數組沒有什么不同; 只是語言提供了一些額外的方法來編寫char類型的對象和它們的數組,並且有一個通用的約定(用strlen這樣的函數系統化),用於解釋如何將存儲在char數組中的數據解釋為字符串的表示。

char status[10];     // declares an array of `char` of length 10. 
char *strPtr;        // declare a pointer to `char`
strPtr = status;     // make `strPtr` point to the first element of `status`

// Declare an array of 6 `char`, and initialize it.
char hello[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

// Shorthand notation for initializing an array of 6 `char` as above
char world[6] = "World";

// I want to store numeric data in this one!
char other[6] = {0, 1, 2, 3, 4, 5};

// "World" is shorthand for a constant array of 6 `char`. This is
// shorthand for telling the compiler to actually put that array in
// memory someplace, and initialize worldPtr to point to that memory.
const char *worldPtr = "World";

// This does the same thing as above. But it's still a *constant* array.
// You should *never* do this; it should be syntactically illegal to
// make a nonconstant `char*` to point to it. This is only allowed for
// historical reasons.
char *helloPtr = "Hello";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM