将数组传递给函数

Passing arrays to functions

本文关键字:函数 数组      更新时间:2023-10-16

我目前在一个介绍性的c++类中,正在处理排序数据的任务。我们最近讨论了structs,我决定使用structs来解决这个问题,而不是创建3个数组来保存数据文件中的信息。

我遇到的问题是当我试图将结构传递给函数时。这是我的错误:

analyze_data.cpp: In function ‘int main()’:
analyze_data.cpp:76: error: conversion from ‘weather*’ to non-scalar type ‘weather’ requested
analyze_data.cpp: In function ‘int find_pos_of_smallest(weather, int, int)’:
analyze_data.cpp:110: error: no match for ‘operator[]’ in ‘data[pos]’
analyze_data.cpp:110: error: no match for ‘operator[]’ in ‘data[pos_of_smallest]’

我不明白第76行的错误。我做了一些关于将结构传递给函数的研究,我发现在类型声明中添加了"&"。然而,我不知道它的作用,也不知道为什么我需要这样做,因为我们还没有在课堂上讨论过它。我也试过,但我只是犯了一系列不同的错误。所以我想我不会发布这些,只是从我所知道的开始。

这是我的代码

/*
Program name: Analyze data
Program discription:  This program will read a data file named data.txt.
This data file is expected to be formated in a specific way and contain
specific weather information.  The program will analyize this data and
return max, min temperatures, 'perfect days', how many cold fronts per
year, 10 coldest  and hottest days in a year and finally find the 5
median days of the year.
Date:  10/1/2012
*/
#include <iostream>
#include <fstream>
using namespace std;
/*
* Declare struct's here
*
*/ 
struct weather
{
string date;
int high;
int low;
};
/* 
*  Forward declaration of a function.  This declares the function,
*  but does not define it.  (Notice that there is no code, just
*  a function header with a semicolon after it.
*
*/
int find_pos_of_smallest (weather data, int start_pos, int end_pos);
/* Our main function.
*
* Parameters:
*    none
*
* Return value:
*    0 if we complete successfully, 1 if there was an error.
*/
int main()
{
//read the data file
ifstream weather_data("data.txt");
//declare array size, and then create array using struct
int days = 365;
weather data[days];
//store the data.txt in the array and then close the file
for (int i=0; i<days; i++)
{
weather_data >> data[i].date;
weather_data >> data[i].high;
weather_data >> data[i].low;
}
weather_data.close();
ofstream data_results ("results.txt");
// create the first 3 lines of the output reults in the following formated
data_results << "Assignment #5n"
<< "CS 1410/2000n"
<< "Jonathan Larsenn";
/*
for(int i=0; i<3; i++)
{
cout<<data[i].date << " "<<data[i].high << " " << data[i].low<<endl;
}
cout<<endl;
*/  
cout<<find_pos_of_smallest(data, 0, days)<<endl;

return 0;  //no error so return a zero  
}//end of program

/**** FUNCTIONS ****/
/* Write down exactly what the function will do (a postcondition).
* Write down what is required to use the function (any preconditions).
* Write down any other behavior or comments that will help a programmer.
*
* Parameters:   (list parameters by type and name, and explain them)
*   int example -- an example parameter
*
* Returns:
*   double -- an example return value
*/
/* Returns the position of the smallest value found in the specified
* subarray.  (Only the elements in the subarray
* between start_pos and end_pos inclusive are checked.)
*
* Parameters:
*    d - a data array
*    start_pos - the first position to check
*    end_pos - the last position to check
*/
int find_pos_of_smallest (weather data, int start_pos, int end_pos)
{
int pos_of_smallest = start_pos;
for (int pos = start_pos+1; pos <= end_pos; pos++)
if (data[pos].low < data[pos_of_smallest].low)
pos_of_smallest = pos;
return pos_of_smallest;
}

您正在向函数传递一个数组。您的函数需要采用类型weather[]weather*。例如:

int find_pos_of_smallest (weather* data, int start_pos, int end_pos)

您应该注意,即使您传递的不是数组而是常规结构,也应该通过引用传递。要么传递指针(weather*)并使用解引用memeber运算符(->),要么传递引用(weather&)并使用普通成员运算符(.)。这是因为传递值(no*/&

您需要将数组作为指针传递:

int find_pos_of_smallest (weather *data, int start_pos, int end_pos)

将参数声明为weather data意味着该结构只有一个实例。将其作为指针意味着您可以传递一个数组(指针将获得数组中第一个元素的地址,当您使用data[pos]对其进行索引时,您将获得该数组中的相关元素)。

每当你声明一个数组时,比如你的data数组,在随后的代码中单独写数组的名称(在本例中是data)实际上相当于写

&(data[0])

意思是"data数组的第一个元素的地址"。这就是数组通常被"传递"到C和C++中的函数中的方式——通过传递它们的第一个元素的地址。这样做是因为在大数组的情况下,传递其他类型参数的通常方式(即复制它们)会非常浪费。它还具有允许函数对数组所做的任何更改对调用代码可见的副作用(有时需要,有时不需要)。

如何在函数中使用该地址?您使用指向-weather:的指针

int find_pos_of_smallest(weather* data, int start_pos, int end_pos)

指针是一个包含其他事物地址的变量。您可以使用*间接运算符、->运算符(如果指向的对象是structclass)以及方便的数组下标运算符[]来访问指向的对象:例如,在find_pos_of_smallest()、内部

data[5]

是指从存储在指针CCD_ 16内的地址开始的CCD_。