如何使用C中的指针将数组(1D)传递给函数?

如何使用C中的指针将数组(1D)传递给函数?,c,arrays,function,pointers,C,Arrays,Function,Pointers,我正在努力完成这个程序的最后一部分,我需要将数组传递给两个不同的函数,但我不知道该怎么做 我遇到的唯一错误是: 在这里: 在这里: //exit, exit; 除此之外,它应该以我需要的方式工作,我知道如何在普通变量上使用指针,但数组的行为不同,我不知道该怎么做 文档已经完成了一半,我仍然需要在描述概述的末尾添加循环,但我只需要传递数组方面的帮助 另外,涉及我的退出线的错误与问题无关,但是如果你知道一个修复方法,那就太好了 /* Description: Do not use global v

我正在努力完成这个程序的最后一部分,我需要将数组传递给两个不同的函数,但我不知道该怎么做

我遇到的唯一错误是: 在这里:

在这里:

//exit,
exit;
除此之外,它应该以我需要的方式工作,我知道如何在普通变量上使用指针,但数组的行为不同,我不知道该怎么做

文档已经完成了一半,我仍然需要在描述概述的末尾添加循环,但我只需要传递数组方面的帮助

另外,涉及我的退出线的错误与问题无关,但是如果你知道一个修复方法,那就太好了

/*
Description: Do not use global variables. Pass your arguments by value and
             by reference. Using arrays and modular programming techniques,
             write a C program that will allow a user to populate an array
             with integers, and then compute and print out the number of
             adjacent pairs in the array (i.e. the number of occurrences where
             an array element is the same as its neighbors). For example, if
             the array contained [2,3,3,4,52,52,4,4,4,4,7,7,1], the number of
             adjacent pairs is 6. The program should give the user the option
             of examining more than one array (i.e. loop). Assume the array has
             a max. of 20 elements. The main() function should primarily be
             responsible to direct the flow of logic.  That is: use one
             function to obtain input (pass by reference), another function to
             do processing (pass by value), and perhaps a third function to do
             output (pass by value).  The main() function should call the input
             function and the processing function. 
*/

//Include statements.
#include <cstdlib>
#include <iostream>
#include <math.h>

//Standard namespace.
using namespace std;

void input (int array[20]); //Used when user inputs the numbers.
void calculate(int array[20], int *pairs); //Used to calculate the matches.
void output(int *pairs); //Used to output the number of pairs.

int main(void) 
{ 
     int array[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
     char quit;

     start:
     int pairs = 0;
     input(array[20]);
     calculate(array[20],&pairs);
     output(&pairs);

     //Ask the user if they want to exit
     printf("\nWould you like to continue testing my project, or exit?");
     printf("\nTo exit input: 'N' or 'n'. To continue testing input anything else.");
     //store their input in variable: exit
     scanf("%s",&quit);
     //If they want to exit...
     if (quit == 'N' || quit == 'n')
     {
         //exit,
         exit;
     }
     //otherwise,
     else
     {
         //clear the screen
         system("cls");
         //and go back to the start.
         goto start;
     }
}

void input(int array[20])
{
     int count = 0;
     for (count;count<20;count++)
     {
         printf("Enter values . . . \n");
         scanf("%i", &array[count]);
     }
}

void calculate(int array[20], int *pairs)
{
     int counter = 0;
     for (counter;counter<19;counter++)
     {
         if (array[counter] == array[counter+1])
            *pairs+=1;
     }
}

void output(int *pairs) 
{
     printf("Number of pairs: [%i]\n", *pairs);
}
/*
描述:不要使用全局变量。通过值和参数传递参数
参考。使用阵列和模块化编程技术,
编写一个允许用户填充数组的C程序
使用整数,然后计算并打印出
阵列中的相邻对(即
数组元素与其相邻元素相同)。例如,如果
数组包含[2,3,3,4,52,52,4,4,4,4,7,1],其中
相邻对为6。程序应为用户提供选项
用于检查多个数组(即循环)。假设该数组
a最多20个元素。main()函数主要应该是
负责指导逻辑流程。那就是:使用一个
函数获取输入(通过引用传递),另一个函数
做处理(按值传递),也许还有第三个函数要做
输出(按值传递)。main()函数应该调用输入
函数和处理函数。
*/
//包括声明。
#包括
#包括
#包括
//标准名称空间。
使用名称空间std;
无效输入(int数组[20])//用户输入数字时使用。
void计算(int数组[20],int*对)//用于计算匹配项。
无效输出(int*对)//用于输出对数。
内部主(空)
{ 
int数组[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
退出;
开始:
整数对=0;
输入(数组[20]);
计算(数组[20],&对);
输出(&对);
//询问用户是否要退出
printf(“\n要继续测试我的项目,还是退出?”);
printf(“\N要退出输入:'N'或'N'。要继续测试输入,请执行其他操作。”);
//将其输入存储在变量exit中
scanf(“%s”,退出(&F);
//如果他们想退出。。。
如果(退出='N'| |退出='N')
{
//退出,
出口
}
//否则,,
其他的
{
//清除屏幕
系统(“cls”);
//回到起点。
转到开始;
}
}
无效输入(整数数组[20])
{
整数计数=0;

对于(count;count调用函数时不传入大小

input(array);
这应该足够了


另一个解决方案是使函数采用
int*
。您可以添加一个额外的参数来传递数组的大小

void func(int* my_array, int size);
您可以这样声明您的数组:

int i[20];
然后像这样调用函数:

func(i, 20);


目前,通过传入
array[20]
,您将返回一个
int
,如果您想知道的话,您将超出范围,因为最大索引是
19
。表达式的不适当返回类型不允许您编译程序。

int*my_array
int my_array[20]
在函数参数列表中完全相同,我不明白你所说的“返回一个
int
”是什么意思@MattMcNabb我和你一样迷茫。我在一年多前写下了这个答案。
exit;
应该是
返回0;
func(i, 20);