简体   繁体   English

C 指针,了解如何将 arrays 传递给 function

[英]C Pointers, understanding how to pass arrays to function

I'm currently revising on C pointers for an upcoming test and I came across a rather interesting program as I was running through the lecture slides.我目前正在为即将进行的测试修改 C 指针,并且在浏览讲座幻灯片时遇到了一个相当有趣的程序。

#include <stdio.h>
#include <ctype.h>

void convertToUppercase(char *sPtr);//function prototype, input is a dereferenced pointer

int main(void)
{
    char string[] = "cHaRaCters and $32.98";

    printf("The string before conversion is %s\n", string);
    convertToUppercase(string); //Why can I just input the string array into the function? I don't need a pointer?
    printf("The string after conversion is %s", string);
}

void convertToUppercase(char *sPtr)
{
    while (*sPtr != '\0')//Clearly see the NULL terminating character, means the entire array is passed to the function.
    {
        *sPtr = toupper(*sPtr);
        sPtr++;
    }
}

What I don't understand is this particular line: convertToUppercase( string );我不明白的是这一行: convertToUppercase( string );

The function prototype requires a (char *sPtr) as an input, but for this case, the string array is just passed into the function call. function 原型需要 (char *sPtr) 作为输入,但在这种情况下,字符串数组只是传递到 function 调用中。 How?如何?

I don't really understand how this works.我真的不明白这是如何工作的。 Can anyone help me to understand this code better?谁能帮助我更好地理解这段代码?

My guess is that the array name itself 'string' already contain the address of the entire array itself (That's why when we assign pointers to array we don't put the '&')我的猜测是数组名称本身的“字符串”已经包含整个数组本身的地址(这就是为什么当我们将指针分配给数组时我们不放“&”)

Thank you in advance, take care!提前谢谢你,保重!

Function parameters having array types are adjusted by the compiler to pointer types to the array element types. Function 具有数组类型的参数由编译器调整为指向数组元素类型的指针类型。

So for example these two function declarations例如这两个 function 声明

void convertToUppercase(char sPtr[]);

and

void convertToUppercase(char *sPtr);

declare the same one function.声明相同的 function。

You may include the both declarations in your program though the compiler can issue a message that there are redundant declarations of the same function.您可以在程序中包含这两个声明,尽管编译器会发出一条消息,指出存在相同 function 的冗余声明。

On the other hand, array designators used in expressions with rare exceptions (as for example using in the sizeof operator) are converted to pointers to their first elements.另一方面,在表达式中使用的数组指示符(例如在sizeof运算符中使用)被转换为指向它们的第一个元素的指针。

So this function call所以这个 function 调用

convertToUppercase(string)

is equivalent to the following function call相当于下面的 function 调用

convertToUppercase( &string[0] )

Pay attention to that it will be better to declare and define the function the following way请注意,最好通过以下方式声明和定义 function

char * convertToUppercase( char s[] )
{
    for ( char *p = s; *p; ++p )
    {
        *p = toupper( ( unsigned char )*p );
    }

    return s;
}

Arrays auto-convert to pointers when passed to functions. Arrays 在传递给函数时自动转换为指针。 It just does, as if you a had written convertToUppercase((char*)string);它只是这样做,就好像你写过convertToUppercase((char*)string); . . Just one more implicit type conversion rule.只是一种隐式类型转换规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM