简体   繁体   English

如何将数组作为参数传递给函数?

[英]How do I pass an array as an argument to a function?

I have read the post here on the same subject, but it doesn't seem to have the solution to my problem. 我在这里阅读了有关同一主题的文章,但似乎并没有解决我的问题的方法。

If I need to write a function str_reverse() which reverses any string passed to it, how can I go about it? 如果我需要编写一个函数str_reverse()来反转传递给它的任何字符串,我该如何处理? Just as the person who asked the question I linked above, the following code 就像问我上面链接的问题的人一样,以下代码

#include <stdio.h>
#include <string.h> //for strlen

#define maxL 300 //Max String Length

void str_reverse(char);

int main(){

    //Variables
    char x[maxL];

    //User Prompt
    printf("Enter a string no longer than %d characters: ", maxL);
    gets(x);
    str_reverse(x);

    //Return Statement
    return 0;
}

void str_reverse(char x){
    int i, l;
    l = strlen(x);
        printf("In reverse: ");
        for(i=l-1; i>=0; i--)
            printf("%c",x[i]);
}

gives me an error. 给我一个错误。 How can I create a program which allows me to reverse the string? 如何创建允许我反转字符串的程序?

You are passing fine but the function isn't receiving properly. 您的传递很好,但该函数接收不正确。 Change it to: 更改为:

void str_reverse(char *x){
..
}

and the prototype to: 和原型:

void str_reverse(char*);

Also, gets() is dangerous and should never be used. 另外, gets()是危险的,永远不要使用。 Use fgets() instead: 使用fgets()代替:

if( fgets(x, sizeof x, stdin) == NULL) { /* failure */ }

x[strcspn(x,"\n")] = 0; // to remove the trailing newline, if any.

Your can pass array as type arr[] or as type * pointer. 您可以将数组作为arr []类型或*类型指针传递。 you will have to pass length as an argument to your string reverse function. 您将必须将长度作为参数传递给字符串反向函数。 New declaration of string reverse will look like this: 字符串反向的新声明将如下所示:

void str_reverse(char *x,int length)

or 要么

void str_reverse(char x[],int length)

for more information you can follow Length of array in function argument 有关更多信息,您可以在函数参数中关注数组的长度

A function parameter declared as an array is adjusted to pointer to array element. 声明为数组的函数参数将调整为指向数组元素的指针。

Thus these function declarations are equivalent and declare the same one function 因此,这些函数声明是等效的,并且声明相同的一个函数

#define maxL 300 
void str_reverse(char[maxL]);

or 要么

void str_reverse(char[10]);

or 要么

void str_reverse(char[1000]);

or 要么

void str_reverse(char[]);

or 要么

void str_reverse( char *);

And on the other hand when an array is passed as an argument to a function it is implicitly converted to pointer to its first element. 另一方面,当数组作为参数传递给函数时,它将隐式转换为指向其第一个元素的指针。

So this function call 所以这个函数调用

char x[maxL];
//...
str_reverse(x);

can be imagine like 可以想像

char x[maxL];
//...
char *tmp = x;
str_reverse(tmp);

Take into account that function gets is unsafe and is not supported by the C Standard any more. 考虑到功能gets是不安全的,并且C标准不再支持该功能。

Use instead standard function fgets 改用标准函数fgets

Also it is better if the function returned pointer to its first character. 如果函数返回指向其第一个字符的指针,也会更好。

Your function does not try to reverse a string. 您的函数不会尝试反转字符串。 It tries to output a string in the reverse order. 它尝试以相反的顺序输出字符串。

So the function that indeed reverses a string can be written like 因此,确实可以反转字符串的函数可以这样写:

char * str_reverse( char *s )
{
    size_t n = strlen( s );

    for ( size_t i = 0; i < n / 2; i++ )
    {
        char c = s[i];
        s[i] = s[n- i-1];
        s[n-i-1] = c;
    }  

    return s;
}

Here is a demonstrative program 这是一个示范节目

#include <stdio.h>
#include <string.h>

#define maxL 300 //Max String Length

char * str_reverse( char *s )
{
    size_t n = strlen( s );

    for ( size_t i = 0; i < n / 2; i++ )
    {
        char c = s[i];
        s[i] = s[n- i-1];
        s[n-i-1] = c;
    }  

    return s;
}

int main( void ) 
{
    //Variables
    char s[maxL];

    //User Prompt
    printf("Enter a string no longer than %zu characters: ", maxL);
    fgets( s, maxL, stdin );
    s[strcspn( s, "\n" )] = '\0';

    puts( str_reverse( s ) );

    return 0;
}

Its output might look like 它的输出可能看起来像

Enter a string no longer than 300 characters: Hello, World!
!dlroW ,olleH

You can achieve this by many different ways: 您可以通过许多不同的方法来实现:

Option-1 Formal parameters as a pointer − 选项1形式参数作为指针-

void myFunction(char *param) {
   .
   .
   .
}

Option-2 Formal parameters as a sized array − 选项2形式参数作为大小数组-

void myFunction(char param[10]) {
   .
   .
   .
}

Option-3 Formal parameters as an unsized array − 选项3形式参数作为未调整大小的数组-

void myFunction(char param[]) {
   .
   .
   .
}

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

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