简体   繁体   English

将数组传递给函数C ++

[英]Passing an array into a function c++

so I'm having an issue passing an entire array of histograms into a function in C++ 所以我在将整个直方图数组传递到C ++中的函数时遇到问题

the arrays are declared like this 这样声明数组

TH1F *h_Energy[2];
h_Energy[0] = new TH1F("h1", "h1", 100, 0, 100);
h_Energy[1] = new TH1F("h2", "h2", 100, 0, 100);

And here is what I'm trying to do in the function: 这是我要在函数中执行的操作:

void overlayhists(TH1 *hists, int numhists) {
    int ymax = 0;
    for (int i=0; i<numhists; i++) {
        if (hist[i].GetMaximum() > ymax) {
            ymax = (hist[i].GetMaximum())*1.05;
        }
     }
}

And I'm passing the function an array like this 我正在向函数传递这样的数组

overlayhists(*h_Energy, 2);

Where h_Energy is an 1D array with 2 elements. 其中h_Energy是具有2个元素的一维数组。 The code will run through the first histogram in the loop but as soon as it starts the second loop and tries to access hist[i].GetMaximum() on the second try it segfaults. 该代码将在循环中的第一个直方图中运行,但是一旦它开始第二个循环并尝试在第二次尝试中访问hist [i] .GetMaximum()则将其分段。

What gives? 是什么赋予了?

This creates an array of pointers to type TH1F 这将创建一个指针数组以键入TH1F

TH1F *h_Energy[2]; //edited after OP changed 

If you want to use this, and subsequently pass it as an argument You must first initialize it, and then create your function prototype to accommodate: 如果要使用此方法,然后将其作为参数传递,则必须首先对其进行初始化,然后创建函数原型以适应:

void overlayhists(TH1F **hists, int numhists);  
                      ^^

From what you have shown above, you would call it like this: (after your initializations) 根据上面显示的内容,您将这样称呼它:(初始化之后)

h_Energy[0] = new TH1F("h1", "h1", 100, 0, 100);
h_Energy[1] = new TH1F("h2", "h2", 100, 0, 100);

overlayhists(h_Energy, 2);

1. Passing any array to function in c++ to change the content: 1.将任何数组传递给c ++函数以更改内容:

Refer to this code snippet: 请参考以下代码片段:

//calling: //调用:

int nArr[5] = {1,2,3,4,5};

Mul(nArr, 5);

Whenever you pass an array to function you actually pass the pointer to first element of the array. 每当将数组传递给函数时,实际上就将指针传递给数组的第一个元素。 This is implicit to C++ and C. If you pass normal value(non array) it will be considered as pass by value though. 这对于C ++和C是隐式的。但是,如果您传递正常值(非数组),则它将被视为按值传递。


// Function Mul() declaration and definition //函数Mul()的声明和定义

void MUl(int* nArr, size_t nArrSize){

 size_t itr = 0;

 for(;itr<nArrSize; itr++)
      nArr[i] = 5*nArr;// here we've coded to multiply each element with 5
}

2. Passing any Ptr to function in c++ to change what pointer is pointing to: 2.将任何Ptr传递给c ++中的函数以更改指向的指针:

Now let us suppose we want to copy nArr (from above code snippet) to another array, say nArrB 现在让我们假设我们想将nArr(从上面的代码片段中)复制到另一个数组,例如nArrB

The best way for a beginner would be to use reference to the pointer. 初学者最好的方法是使用对指针的引用。 You can pass reference to the pointer to your function //so we had 您可以传递对函数指针的引用///

int nArr[5] = {1,2,3,4,5}; int *nArrB;

Here we don't know the gonnabe size of nArrB. 在这里,我们不知道nArrB的大小。 to copy nArr to nArrB we have to pass nArr, address of pointer to nArrB(or reference to pointer of nArrB or pointer to pointer of nArrB) and size of array. 要将nArr复制到nArrB,我们必须传递nArr,指向nArrB的指针的地址(或指向nArrB的指针或指向nArrB的指针的引用)和数组的大小。 Here is the implementation. 这是实现。

//Calling //调用

CopyNArr(nArr, &nArrB, 5);


//Function implementation //函数实现

void CopyNArr(int* nArr, int* & nArrB, size_t nArrSize) {

// dymanically allocating memory size for array. Assuming 4 byte int size

 nArrB = new int[nArrSize*4];
 size_t itr = 0;
 //Copying values
 for(;itr<nArrSize; itr++)
      nArrB[i] = nArr[i];

} }

//After copy nArrB is pointing to first element of 5 element array. //复制后,nArrB指向5个元素数组的第一个元素。


I hope it helped. 希望对您有所帮助。 Write for any further clarification. 写进一步的澄清。

You have an array of size 2, but you've created only one element. 您有一个大小为2的数组,但是您仅创建了一个元素。 And that one with a wrong index. 而且那个索引错误。 Array indexing starts with 0. 数组索引从0开始。

The elements should be at h_histogram[0] and h_histogram[1] . 元素应位于h_histogram[0]h_histogram[1]

I am sorry if this answer is completely irrelevant but I am tempted to post it. 很抱歉,如果这个答案完全无关紧要,但我很想发布。 These is an experiment I have done after seeing your question. 这些是我在看到您的问题后所做的实验。

#include<iostream>
using namespace std;
main()
{
int e[2]={0,1};
int *p[2];
int i;
/*
  Printing the array e content using one pointer
  from an array of pointers. Here I am not using p[2]
  at all.
*/
p[1]=e;
cout<<"Elements of e are : \n";
for(i=0;i<2;i++)
{
cout<<*(p[1]+i)<<endl;
/*
In the above line both *((*p)+i) and *(p+i)
won't serve the purpose of printing the array values.
*/
}
/*Printing the array e content using pointer to array*/
cout<<"Elements of e are : \n";
for(i=0;i<2;i++)
{
cout<<*(e+i)<<endl;
}
/*Note that pointer to array is legal but array TO pointer
(don't confuse with array OF pointers) is not.*/
}

Hope this will refresh your understanding. 希望这会刷新您的理解。

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

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