我只是试图理解如何在一个字符串数组中寻找单个字符。同时,这当然也能让我理解指向指针的下标操作。
如果有char **a
,我想要访问第二个字符串的第三个字符,这个表达式是否正确:**((a+1)+2)
?看起来应该是可以的...
几乎正确,但不完全正确。 正确答案是:
*((*(a+1))+2)
因为你需要首先对实际字符串指针之一进行解引用,然后将所选的字符串指针向下解引用到所需字符。 (请注意,我添加了额外的括号以便清楚地表示操作顺序)。
或者,这个表达式:
a[1][2]
这种写法也可以运行!或许更受欢迎,因为你所尝试做的意图更加自明,而且符号本身更加简洁。对于刚接触该语言的人来说,这种形式可能不是立即显而易见的,但要理解数组符号之所以有效是因为在C语言中,数组索引操作实际上只是等价指针操作的简写。即:*(a+x)和a[x]是相同的。因此,通过将这种逻辑扩展到原始问题,有两个单独的指针解引用操作级联在一起,表达式a[x][y]等同于*((*(a+x))+y)的一般形式。
您不必使用指针。
int main(int argc, char **argv){
printf("argv[1]的第三个字符是[%c]。\n", argv[1][2]);
}
然后:
$ ./main hello argv[1]的第三个字符是[l]。
那是一个数字1和字母l。
如果您想要,可以使用指针...
*(argv[1] +2)
或者甚至是
*((*(a+1))+2)
就像上面有人指出的那样。
这是因为数组名称是指针。
#include <stdio.h>
int main()
{
char str_a[20];
str_a[0] = 'H';
str_a[1] = 'e';
str_a[2] = 'l';
str_a[3] = 'l';
str_a[4] = 'o';
str_a[5] = ',';
str_a[6] = ' ';
str_a[7] = 'w';
str_a[8] = 'o';
str_a[9] = 'r';
str_a[10] = 'l';
str_a[11] = 'd';
str_a[12] = '!';
str_a[13] = '\n';
str_a[14] = 0;
printf(str_a);
}
#include <stdio.h>
#include <string.h>
int main()
{
char str_a[20];
strcpy(str_a, "Hello, world!\n");
printf(str_a);
}
查找有关C字符串的更多信息
http://www.cs.uic.edu/~jbell/CourseNotes/C_Programming/CharacterStrings.html
http://www.tutorialspoint.com/cprogramming/c_strings.htm
指针
"EIP寄存器是一个指针,通过包含其内存地址,在程序执行期间“指向”当前指令。指针的概念也被用于C语言中。由于物理内存实际上无法移动,其中的信息必须被复制。将大块内存复制以供不同函数或不同位置使用可能非常计算密集。从内存角度来看,这也很昂贵,因为必须在源代码复制之前保存或分配新目标复制的空间。指针是解决此问题的一种方法。与复制大块内存不同,传递该块内存开头的地址更加简单。"。
"C语言中的指针可以像任何其他变量类型一样定义和使用。由于x86架构上的内存使用32位寻址,因此指针的大小也为32位(4字节)。通过在变量名前面添加星号(*),可以定义指针。不需要定义该类型的变量,而是定义一个指向该类型数据的指针。pointer.c程序是使用char数据类型的指针的示例,该类型大小仅为1字节"。
#include <stdio.h>
#include <string.h>
int main()
{
char str_a[20]; // A 20-element character array
char *pointer; // A pointer, meant for a character array
char *pointer2; // And yet another one
strcpy(str_a, "Hello, world!\n");
pointer = str_a; // Set the first pointer to the start of the array.
printf(pointer);
pointer2 = pointer + 2; // Set the second one 2 bytes further in.
printf(pointer2); // Print it.
strcpy(pointer2, "y you guys!\n"); // Copy into that spot.
printf(pointer); // Print again.
}
reader@hacking:~/booksrc $ gcc -o pointer pointer.c
reader@hacking:~/booksrc $ ./pointer
Hello, world!
llo, world!
Hey you guys!
reader@hacking:~/booksrc $
#include <stdio.h>
int main()
{
int int_var = 5;
int *int_ptr;
int_ptr = &int_var; // put the address of int_var into int_ptr
}
#include <stdio.h>
int main()
{
int int_var = 5;
int *int_ptr;
int_ptr = &int_var; // Put the address of int_var into int_ptr.
printf("int_ptr = 0x%08x\n", int_ptr);
printf("&int_ptr = 0x%08x\n", &int_ptr);
printf("*int_ptr = 0x%08x\n\n", *int_ptr);
printf("int_var is located at 0x%08x and contains %d\n", &int_var, int_var);
printf("int_ptr is located at 0x%08x, contains 0x%08x, and points to %d\n\n", &int_ptr, int_ptr, *int_ptr);
}
http://cslibrary.stanford.edu/106/
http://www.programiz.com/c-programming/c-dynamic-memory-allocation
数组
这里有一篇由名叫Alex Allain的人撰写的关于多维数组的简单教程,链接在这里:http://www.cprogramming.com/tutorial/c/lesson8.html
这里有一篇由名叫Todd A Gibson的人撰写的关于数组的信息,链接在这里:http://www.augustcouncil.com/~tgibson/tutorial/arr.html
遍历一个数组
#include <stdio.h>
int main()
{
int i;
char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
int int_array[5] = {1, 2, 3, 4, 5};
char *char_pointer;
int *int_pointer;
char_pointer = char_array;
int_pointer = int_array;
for(i=0; i < 5; i++) { // Iterate through the int array with the int_pointer.
printf("[integer pointer] points to %p, which contains the integer %d\n", int_pointer, *int_pointer);
int_pointer = int_pointer + 1;
}
for(i=0; i < 5; i++) { // Iterate through the char array with the char_pointer.
printf("[char pointer] points to %p, which contains the char '%c'\n", char_pointer, *char_pointer);
char_pointer = char_pointer + 1;
}
}
链表 vs 数组
数组并不是唯一的选择,这里提供关于链表的信息。
http://www.eternallyconfuzzled.com/tuts/datastructures/jsw_tut_linklist.aspx
结论
这篇文章仅仅是为了传递我在研究这个主题时阅读到的一些信息,希望能对他人有所帮助。
据我所知,字符串实际上是字符数组,因此这应该可以工作:
```a[1][2]
int array[5]; /* Declares 5 contiguous (per Plauger Standard C 1992) integers */
int *ptr = array; /* Arrays can be used as pointers */
ptr[0] = 1; /* Pointers can be indexed with array syntax */
*(array + 1) = 2; /* Arrays can be dereferenced with pointer syntax */
尝试使用a[1][2]
或者*(*(a+1)+2)
。
基本上,数组引用是指针解引用的语法糖。a[2]与a+2相同,也与2[a]相同(如果你真的想要不可读的代码)。字符串数组与双指针相同。因此,您可以使用a[1]或*(a+1)
提取第二个字符串。然后,您可以使用b[2]或*(b + 2)
在该字符串中找到第三个字符(现在称其为'b')。将原始的第二个字符串替换为“b”,我们最终得到了a[1][2]或*(*(a+1)+2)
。