繁体   English   中英

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

[英]How do I pass an array(1D) to a function using pointers in C?

我在该程序的最后一步很挣扎,我需要将数组传递给两个不同的函数,但我不知道该怎么做。

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

 input(array[20]);
 calculate(array[20],&pairs);

和这里:

//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);
}

调用函数时,您不会传递大小。

input(array);

那应该足够了。


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

void func(int* my_array, int size);

您可以这样声明数组:

int i[20];

然后像这样调用您的函数:

func(i, 20);

当前,通过传入array[20] ,您将返回一个int并且您会超出范围,以防万一,因为最大索引为19 表达式的不正确的返回类型不允许您编译程序。

暂无
暂无

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

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