我刚刚在我的大学开始了一门新课程,它没有学习所有的语法和语义(这并不算太糟)。然而,与我理解的语言(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