[英]Having difficulty understand how this piece of code works(double pointers)
My teacher gave me a correction of an exercise and it contains code using double pointers. 我的老师给了我一个练习的更正,其中包含使用双指针的代码。 Since I am fairly new to CI have difficulty comprehending what the code does.
由于我对CI相当陌生,因此很难理解代码的作用。 I know the basics of a single pointer ,but somehow I lose my way on the double pointer route.
我知道单指针的基础知识,但是不知怎么在双指针路线上迷路了。
The code: 编码:
#include <stdio.h>
main(int argc, char** argv){
printf("\nHello ");
char** runner = argv;
++runner;
while(*(runner+2) != 0){
**runner = toupper(**runner);
printf("%s, ",*runner);
++runner;
}
**runner = toupper(**runner);
printf("%s ",*runner);
++runner;
**runner = toupper(**runner);
printf("and %s!",*runner);
}
-The first issue I have is understanding why the main function uses a double pointer? -我遇到的第一个问题是了解为什么主函数使用双指针? -The second issue ,after initializing the double pointer ,runner, it is being told to point to one place further.
-第二个问题,初始化双指针运行器后,被告知进一步指向一个位置。 But how on earth can you know where it points to ,if it points to a pointer which in its own turn points to a place you have no idea of?
但是,如果它指向一个指针,而指针又指向您不知道的位置,那么您怎么知道它指向的位置呢?
Thank you for your time 感谢您的时间
Let's try to explain it: 让我们尝试解释一下:
main(int argc, char** argv){
Normal main function just there is the returntype missing that may cause problems. 正常的main函数只是缺少可能导致问题的returntype。 (c90 vs c99 AFIK).
(c90 vs c99 AFIK)。 The first parameter is the count of parameters which is >1 (because the first parameter is the binary name (helpful for multi call applications like busybox)).
第一个参数是大于1的参数计数(因为第一个参数是二进制名称(对于像busybox这样的多调用应用程序很有帮助))。 The second parameter are the parameter as a string aka char-array, which comes for the shell which called this binary.
第二个参数是作为字符串(也称为char-array)的参数,它用于名为此二进制文件的shell。
printf("\nHello ");
char** runner = argv;
++runner;
printing out hello with skipping the first argument of that binary (which is the binary name) 通过跳过该二进制文件的第一个参数(二进制名称)来打个招呼
while(*(runner+2) != 0){
Checking if the third byte of the current parameter is the null byte. 检查当前参数的第三个字节是否为空字节。 IMHO this can cause problems if there is no second parameter.
恕我直言,这可能会导致问题,如果没有第二个参数。
**runner = toupper(**runner);
Converting it to uppercase. 将其转换为大写。
printf("%s, ",*runner);
Printing out that parameter 打印该参数
++runner;
Jumping to the next argument. 跳到下一个参数。
}
**runner = toupper(**runner);
printf("%s ",*runner);
++runner;
**runner = toupper(**runner);
printf("and %s!",*runner);
Almost the same as in the loop just it takes the next two parameter which comes after a 2 byte parameter. 几乎与循环中的相同,只是它采用了2个字节参数之后的下两个参数。
When an array of pointers is passed to a function, it "decays" to a pointer to pointer. 当将指针数组传递给函数时,它“衰减”为指向该指针的指针。 This is precisely what happens with
argv
of main
: it is a pointer to pointers to char. 这恰恰与之发生
argv
的main
:它是一个指针的指针为char。 Sometimes, the main
declaration is written as the equivalent 有时,
main
声明被写为等效的
int main(int argc, char *argv[])
to be more explicit about passing an array of pointers. 更明确地传递指针数组。
With the understanding of what's going on, it is easy to see that 通过了解正在发生的事情,很容易看到
char** runner = argv;
++runner;
is logically equivalent to this: 在逻辑上等效于:
char** runner = &argv[1];
The author simply skips over the initial argument of main
, and go straight to the command arguments proper, which start at index 1
. 作者只需跳过
main
的初始参数,然后直接转到从索引1
开始的正确的命令参数。
Why the main function uses a double pointer? 为什么主函数使用双指针?
The main function receives 2 arguments: the number of arguments passed to the command line ( argc
means arguments count) and an array containing the arguments. 主函数接收2个参数:传递给命令行的参数数量(
argc
表示参数计数)和包含参数的数组。
An argument is a string, so it's a char*
in C. And you have an array of strings, so the type of argv
is char**
. 参数是一个字符串,因此在C中为
char*
。并且您有一个字符串数组,因此argv
的类型为char**
。
The second issue ,after initializing the double pointer ,runner, it is being told to point to one place further. 第二个问题,在初始化双指针运行器之后,被告知要进一步指向一个地方。 But how on earth can you know where it points to ,if it points to a pointer which in its own turn points to a place you have no idea of?
但是,如果它指向一个指针,而指针又指向您不知道的位置,那么您怎么知道它指向的位置呢?
runner
points to the same array as pointed by argv
. runner
指向argv
指向的数组。 But now when you do ++runner
, it will point to the next element in the array of arguments. 但是现在,当您执行
++runner
,它将指向参数数组中的下一个元素。 In C, the first string is actually the name of the command, so runner
now points to the first argument passed in the command line. 在C语言中,第一个字符串实际上是命令的名称,因此
runner
现在指向在命令行中传递的第一个参数。
I havent run your code but I can give you some inside on why a double pointer is used. 我还没有运行您的代码,但是我可以深入了解为什么使用双指针。
Argv holds the arguments. Argv保留参数。 Argv[0] is the program name, argv[1] is the first argument, etc. So that is why the "++runner;"
Argv [0]是程序名称,argv [1]是第一个参数,依此类推。这就是为什么“ ++ runner;”的原因。 is incremented first to access the first argument.
首先递增以访问第一个参数。
Double pointers in this case allow you to index locations in memory by easily incrementing and then when you get there you access your data which in turn it happens to be another char*. 在这种情况下,双指针使您可以通过轻松地递增来索引内存中的位置,然后在到达该位置时访问数据,而数据又恰好是另一个字符*。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.