我剛剛在我的大學開始了一門新課程,它沒有學習所有的語法和語義(這並不算太糟)。然而,與我理解的語言(java,python)相比,一個很大的區別是指針的概念。理解C中字符串的指針
我知道:
& - Address of something
* - value stored at the address of something
所以,如果我有這樣的字符串:
char a[] = "ABCDEF";
是 'A' 與它關聯的地址(&一),如果我這樣做(* a)它是否引用整個字符串? '數組'(A)中的第一個字符?
想想看看它是一個char [],字符串中的每個單個字符都有自己的地址嗎?
我最終的目標是編寫一個函數,該函數可以操縱指針,以便找到與第二個字符串中的字符匹配的一個字符串中的第一個字符。
我在正確的軌道上嗎?請注意,這是所有的僞代碼,因爲我還在努力學習C語法
#include <stdio.h>
int main() {
create address to 'a'
create address to 'b'
make 'a' a string like "abcdefg"
make 'b' a string like 'b'
call findMatch(a,b); //pass in both to a function
return 0; // I know I have to have this at the end
}
void findMatch(char a, char b){
Retrieve the pointer to the first character in the 'a' string
Increment through 'a' to see if it matches the dereferenced b
If 'a' contains 'b' print its in the string as well as the address of the location in 'a'
}
example run - findMatch("abcdef","f") gives a print statement that says 'f' is in 'abcdef' and the address location of 'f'
我看過有建立在C字符串函數庫,但我要處理的指針自己學習。
由於
問題是C *沒有*字符串。我們只有字符數組,並按照慣例在字符串的最後一個重要字符之後放置了一個「\ 0」。 – 5gon12eder 2014-09-30 01:12:22
你基本上不得不相信有這樣的東西作爲一個「串」 - 沒有。內存中可能有字節,你認爲*是一個字符串,* *像字符串一樣對待,但對於系統來說,它們也可能代表一個蕪菁。那麼,什麼是字符串純粹取決於你要跟蹤。而且,由於沒有什麼可以調用「字符串」,因此'char *'指針只是指向RAM中某處的指針。你是否可以認爲它是一個「字符串」,純粹是你如何處理它的問題。 – 2014-09-30 01:12:48
因此,在「abcd」的字符數組中,是否有一種方法可以引用「a」並通過數組增加它?謝謝 – asdf 2014-09-30 01:14:56