为什么必须使用二维数组?

发布于 2025-01-10 18:15:28 字数 488 浏览 0 评论 0 原文

我正在查看这段代码,它将大写字母转换为小写字母,我不明白为什么它声明 char*argv[] ,然后在 for 循环中使用 argv[1] [i] 就好像它是一个二维数组一样。 欢迎任何提示,谢谢。


#include <stdio.h>

int main(int argc, char*argv[]){
    if(argc>1) {
        int i;
        char c;
        for(i=1; (c=argv[1][i]) != '\0'; i++){
            if('A'<=c && c<='Z')
                putchar(c+'a'-'A');
            else
                putchar(c);
        }
        putchar('\n');
    }
}

I'm reviewing this code which converts the capital letters to lower case and I don't understand why it declares char*argv[] and later inside the for loop it uses argv[1][i] as if it were a two-dimensional array.
Any tip is welcomed, thanks.


#include <stdio.h>

int main(int argc, char*argv[]){
    if(argc>1) {
        int i;
        char c;
        for(i=1; (c=argv[1][i]) != '\0'; i++){
            if('A'<=c && c<='Z')
                putchar(c+'a'-'A');
            else
                putchar(c);
        }
        putchar('\n');
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雅心素梦 2025-01-17 18:15:28

argv 实际上并不是一个二维数组,而是一个 char * 数组。每个指针都指向一个字符串,每个字符串都是一个 char 数组,因此可以像二维数组一样访问它。

更具体地说,argv 的每个成员都是传递给程序的命令行参数,第一个成员通常是程序的名称。这些参数是字符串,每个字符串都是一个字符数组。因此 argv[1][i] 会从第一个命令行参数中获取第 i 个字符。

argv is not actually a 2 dimensional array but an array of char *. Each of those pointers point to a string, each of which is a char array, so it can be accessed the same way a 2 dimensional array is.

More specifically, each member of argv is a command line argument passed to the program, with the first member conventionally being the name of the program. Those arguments are strings, and each string is an array of characters. So argv[1][i] grabs the ith character from the first command line argument.

GRAY°灰色天空 2025-01-17 18:15:28

从main中第二个参数的声明可以看出,

char*argv[]

它是一个char *类型的指针数组。

请注意,具有数组类型的函数参数会被编译器调整为指向数组元素类型的指针。

这两个声明

int main(int argc, char*argv[]){

所以main和

int main(int argc, char**argv){

是等价的。

因此,如果您有一个指针数组,例如

char * s[] = { "Hello", "World" };

,那么该数组包含两个指向字符串文字 "Hello""World" 的第一个字符的指针。

因此,要输出数组的元素,您可以使用这些嵌套的 for 循环。

for ( size_t i = 0; i < sizeof( s ) / sizeof( *s ) /* == 2 */; ++i )
{
    for ( size_t j = 0; s[i][j] != '\0'; ++j )
    {
        putchar( s[i][j] );
    }
    putchar( ' ' );
}

putchar( '\n' );

要理解内部循环,请考虑以下代码片段

char *p = "Hello";
for ( size_t i = 0; s[i] != '\0'; i++ )
{
    [utchar( s[i] );
}
putchar( '\n' );

在第一个代码片段中,您有一个由两个此类指针组成的数组。

As it is seen from the declaration of the second parameter in main

char*argv[]

it is an array of pointers of the type char *.

Pay attention to that function parameters having array types are adjusted by the compiler to pointers to array element types.

So these two declarations of main

int main(int argc, char*argv[]){

and

int main(int argc, char**argv){

are equivalent.

So if you have an array of pointers like for example

char * s[] = { "Hello", "World" };

then this array contains two pointers to first characters of the string literals "Hello" and "World".

So to output elements of the array you can use these nested for loops

for ( size_t i = 0; i < sizeof( s ) / sizeof( *s ) /* == 2 */; ++i )
{
    for ( size_t j = 0; s[i][j] != '\0'; ++j )
    {
        putchar( s[i][j] );
    }
    putchar( ' ' );
}

putchar( '\n' );

To understand the inner loop consider the following code snippet

char *p = "Hello";
for ( size_t i = 0; s[i] != '\0'; i++ )
{
    [utchar( s[i] );
}
putchar( '\n' );

In the first code snippet you have an array of two such pointers.

三生池水覆流年 2025-01-17 18:15:28

当您使用参数运行代码时,它们存储在 char argv 数组中。
这意味着这些值存储在字符数组中(保存字符串值)

每个参数都用空格分隔,例如a.exe hello
如果你这样做 printf("%s", argv[1]);
你会得到结果“hello”,argv[1]的值是整个字符串,如果你想访问该字符串的各个字符,你可以使用多数组表示法argv[1][0] 将为您提供第一个字符,在本例中为 h

When you run your code with parameters, they are stored in the char argv array.
This means that the values are stored in a char array (holding a string value)

Each parameter is separated with spaces e.g. a.exe hello.
If you do printf("%s", argv[1]);
You will get the result "hello", the value of argv[1] is the whole string, if you want to access individual characters of that string you can do it by using multi-array notation argv[1][0] which will give you the first char, in this case, h

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文