如何让函数将指针作为参数接收?

罗伯特·LD

我目前正在尝试完成我的一门大学课程的家庭作业,但被卡住了。

#include <stdio.h>

void add(int *a[], const int *b[], const int *c[], int size); /* Function Prototype */

int main() {
  int a[5];
  const int b[] = {1,2,3,4,5};
  const int c[] = {1,1,1,1,1};
  add(a,b,c,5);

  for(int i=0; i < 5; i++) {
    printf("%d\t", a[i]);
  }
  return 0;
}

void add(int *a[], const int *b[], const int *c[], int size) {
  for (int i = 0; i < size; i++) {
  a[i]=b[i]+c[i];
 }
}

我试图让函数add接收其参数作为指针而不是int数组。我已经咨询过教科书以及一些同行的教科书,但我仍然普遍感到困惑。如果有人可以请问一下为什么我的上述实现不起作用?

这些是我得到的错误:

main.c: In function 'main':
main.c:9:7: warning: passing argument 1 of 'add' from incompatible pointer 
type [-Wincompatible-pointer-types]
   add(a,b,c,5);
   ^
main.c:3:6: note: expected 'int **' but argument is of type 'int *'
 void add(int *a[], const int *b[], const int *c[], int size); /* Function 
Prototype */
  ^~~
main.c:9:9: warning: passing argument 2 of 'add' from incompatible pointer 
type [-Wincompatible-pointer-types]
   add(a,b,c,5);
     ^
main.c:3:6: note: expected 'const int **' but argument is of type 'const int *'
 void add(int *a[], const int *b[], const int *c[], int size); /* Function 
Prototype */
  ^~~
main.c:9:11: warning: passing argument 3 of 'add' from incompatible pointer type [-Wincompatible-pointer-types]
   add(a,b,c,5);
       ^
main.c:3:6: note: expected 'const int **' but argument is of type 'const int *'
 void add(int *a[], const int *b[], const int *c[], int size); /* Function 
Prototype */
  ^~~
main.c: In function 'add':
main.c:19:14: error: invalid operands to binary + (have 'const int *' and 'const int *')
     a[i]=b[i]+c[i];
      ~~~~^~~~~

exit status 1
来自莫斯科的弗拉德

根据C标准(6.7.6.3函数声明符(包括原型))

7参数声明为“类型数组”应调整为“类型的合格指针”,其中类型限定符(如果有)是在数组类型派生的[和]中指定的那些。如果关键字static也出现在数组类型派生的[和]中,则对于函数的每次调用,相应实际参数的值应提供对数组第一个元素的访问,该元素至少具有指定数量的元素通过大小表达式。

因此这些函数声明声明相同的一个函数

void add(int a[5], const int b[5], const int c[5], int size); 
void add(int a[5], const int b[10], const int c[15], int size); 
void add(int a[], const int b[], const int c[], int size); 

并且等同于声明

void add(int *a, const int *b, const int *c, int size); 

考虑到根据相同的C标准,main不带参数的函数应声明为

int main( void )

将魔术“原始”数字设为5也是一个坏主意。

该程序可以看起来像

#include <stdio.h>

void add( int *a, const int *b, const int *c, size_t size); /* Function Prototype */

#define N   5

int main( void ) 
{
    int a[N];
    const int b[N] = { 1, 2, 3, 4, 5 };
    const int c[N] = { 1, 1, 1, 1, 1 };

    add( a, b, c, N );

    for ( size_t i = 0; i < N; i++ ) 
    {
        printf( "%d\t", a[i] );
    }

    return 0;
}

void add( int *a, const int *b, const int *c, size_t size ) 
{
    for ( size_t i = 0; i < size; i++ ) 
    {
        a[i] = b[i] + c[i];
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

这个构造函数代码如何将指针作为参数?

如何将函数指针作为类模板参数传递?

如何构建接受多维数组作为参数的函数,接收不兼容的指针类型

将指针作为函数参数传递

将双指针作为函数参数传递

指针作为函数的参数

如何定义一个函数,该函数接收一个指针作为参数并返回一个指向该参数的子代之一的指针?

如何将指向设备函数的指针作为内核函数的参数传递?

如何制作一个将任意函数指针作为参数的函数?

如何将 lamda 作为成员函数的函数指针参数传递?

如何访问C ++函数,该函数将指针作为来自C#的输入参数

量角器:如何创建将HTML元素作为参数接收的函数?

如何将组件名称作为参数传递并在函数中接收它

Android NDK如何将接收到的DatagramPacket作为参数传递给C函数?

回调:将函数指针作为参数并传递附加的参数

将指针传递给成员函数作为void *函数的参数

将函数转换为通用 void 指针作为函数参数

如何将指向动态分配数组的指针作为函数参数进行传输

如何使用空指针数组作为参数将OCaml绑定到C函数

如何通过原始指针将闭包作为C函数的参数传递?

有关如何使用指针将数组作为函数参数传递的代码片段的解释?

如何将指针传递给在 C++ 中作为引用传递参数的函数

如何将指针/数组作为参数传递给 GTK 回调函数?

如何通过作为参数传递的函数指针调用函数?

指针中的malloc作为参数接收

如何转发声明一个函数,该函数将指向函数的指针作为参数?

如何调用作为参数接收的C函数

如何在 Kotlin 的函数类型中接收 null 作为参数

将函数指针作为非类型模板参数传递