简体   繁体   English

将数组作为参数传递给函数

[英]passing Array as argument to function

i have a doubt with passing array as a argument to function, my code as below, 我对传递数组作为函数的参数有疑问,我的代码如下,

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

  void str_cpy(char a[], char b[])
  {
     a = b;
     printf("%s\n", a);
  }

  int main()
  {
     char a[] = "hello";
     char b[] = "world";

     str_cpy(a, b);
     printf ("%s\n", a);
     return 0;
  }

In the above code i am passing array to function is pass by reference so it should print "world" in the main function because i am assigning value of b to a ie a = b in the function definition but it is not , so please help me .. 在上面的代码中,我将数组传递给函数是通过引用传递,因此它应在主函数中打印“ world”,因为我在函数定义中将b的值赋给a即a = b,但不是,所以请帮忙我 ..

When you pass an array to function. 当您传递数组以起作用时。 It decays as an pointer to the first element of the array. 它衰减为指向数组第一个元素的指针。

Your function str_cpy() only points the passed pointer a to another pointer b . 您的函数str_cpy()仅将传递的指针a指向另一个指针b It does not modify the contents of the original character array a . 它不会修改原始字符数组a的内容。 Note the pointers a and b are local to the function. 请注意,指针ab在函数的本地。

Also, note that basic rule about arrays is: 另外,请注意关于数组的基本规则是:
"Arrays are not assignable, You have to copy each element in array." “数组不可分配,您必须复制数组中的每个元素。”

The code you have is equivalent to : 您拥有的代码等效于

 char a[] = "hello";
 char b[] = "world";

 char *ptr1 = a;
 char *ptr2 = b;

 ptr1 = ptr2;

 printf("[%s]",a);

It does not change the contents of the original array, it merely points an pointer which was pointing to the array to some another array. 它不会更改原始数组的内容,而只是将指向该数组的指针指向另一个数组。

Copying each element of one array to another array is the functionality that strcpy() provides. strcpy()提供的功能是将一个数组的每个元素复制到另一个数组。 You should simply use the standard library provided function. 您应该只使用标准库提供的功能。

Your str_cpy() function could be written as: 您的str_cpy()函数可以写为:

void str_cpy(char *a, char *b)
{
    a = b;
    printf("%s\n", a);
}

This is semantically equivalent to what you wrote; 从语义上讲,这等效于您所写的内容。 there is no operational difference between what you wrote and what I wrote. 你写的和我写的没有操作上的区别。 The arrays are passed as pointers to the first elements of the array. 数组作为指向数组第一个元素的指针传递。 They are conveniently initialized local pointer variables. 它们是方便地初始化的本地指针变量。 Within the scope of the function, you make a point to the same place that b does, but this doesn't affect the arrays in the main() program. 在该函数的范围,你让a点到同一个地方b做,但这并不影响阵列中main()程序。

To copy arrays around in C, you have to copy each element in turn: 要在C中复制数组,您必须依次复制每个元素:

void str_cpy(char *a, char *b)
{
    while ((*a++ = *b++) != '\0')
        ;
    printf("%s\n", a);
}

You are interpreting it as wrong, It's actually pass by value . 您将其解释为错误,它实际上是pass by value

void str_cpy(char a[], char b[]) // it's same as str_cpy(char *a,char *b)
  {
     a = b;
     printf("%s\n", a); // here it will print "world" because it's a local
              // change in a but actually in main a still contains "hello"
  }

Since you are using <string.h> you can directly use strcpy(a,b); 由于使用的是<string.h> ,因此可以直接使用strcpy(a,b);

Or, try something like this to copy char by char into target array. 或者,尝试执行类似的操作,将一个字符一个字符地复制到目标数组中。

str_cpy(char *a,char *b)
{
 while(*b != '\0')
  {
  *a=*b;
  a++;
  b++;
  }
}

But it could be a problem if array a doesn't have enough space to keep the copied character. 但是如果数组a没有足够的空间来保留复制的字符,则可能是一个问题。

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

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