C++ 将数组传递到函数c++;
所以我遇到了一个问题,将整个直方图数组传递到C语言中的函数中++ 数组是这样声明的C++ 将数组传递到函数c++;,c++,arrays,function,pointers,C++,Arrays,Function,Pointers,所以我遇到了一个问题,将整个直方图数组传递到C语言中的函数中++ 数组是这样声明的 TH1F *h_Energy[2]; h_Energy[0] = new TH1F("h1", "h1", 100, 0, 100); h_Energy[1] = new TH1F("h2", "h2", 100, 0, 100); overlayhists(*h_Energy, 2); 下面是我在函数中要做的: void overlayhists(TH1 *hists, int numhists) {
TH1F *h_Energy[2];
h_Energy[0] = new TH1F("h1", "h1", 100, 0, 100);
h_Energy[1] = new TH1F("h2", "h2", 100, 0, 100);
overlayhists(*h_Energy, 2);
下面是我在函数中要做的:
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;
}
}
}
其中,h_能量是一个包含2个元素的一维数组。代码将在循环的第一个柱状图中运行,但一旦它启动第二个循环并尝试在第二个循环中访问hist[i].GetMaximum(),请重试
有什么好处 您有一个大小为2的数组,但只创建了一个元素。还有那个索引错误的。数组索引从0开始
元素应该位于
h\u直方图[0]
和h\u直方图[1]
您有一个大小为2的数组,但您只创建了一个元素。还有那个索引错误的。数组索引从0开始
元素应该位于
h\u直方图[0]
和h\u直方图[1]
您有一个大小为2的数组,但您只创建了一个元素。还有那个索引错误的。数组索引从0开始
元素应该位于
h\u直方图[0]
和h\u直方图[1]
您有一个大小为2的数组,但您只创建了一个元素。还有那个索引错误的。数组索引从0开始
元素应位于
h\u直方图[0]
和h\u直方图[1]
这将创建一个指向TH1F类型的指针数组
TH1F *h_Energy[2]; //edited after OP changed
如果要使用它,然后将其作为参数传递
您必须首先对其进行初始化,然后创建函数原型以适应:
void overlayhists(TH1F **hists, int numhists);
^^
根据上面显示的内容,您可以这样称呼它:(在初始化之后)
这将创建指向TH1F类型的指针数组
TH1F *h_Energy[2]; //edited after OP changed
如果要使用它,然后将其作为参数传递
您必须首先对其进行初始化,然后创建函数原型以适应:
void overlayhists(TH1F **hists, int numhists);
^^
根据上面显示的内容,您可以这样称呼它:(在初始化之后)
这将创建指向TH1F类型的指针数组
TH1F *h_Energy[2]; //edited after OP changed
如果要使用它,然后将其作为参数传递
您必须首先对其进行初始化,然后创建函数原型以适应:
void overlayhists(TH1F **hists, int numhists);
^^
根据上面显示的内容,您可以这样称呼它:(在初始化之后)
这将创建指向TH1F类型的指针数组
TH1F *h_Energy[2]; //edited after OP changed
如果要使用它,然后将其作为参数传递
您必须首先对其进行初始化,然后创建函数原型以适应:
void overlayhists(TH1F **hists, int numhists);
^^
根据上面显示的内容,您可以这样称呼它:(在初始化之后)
如果这个答案完全不相关,我很抱歉,但是 我很想把它贴出来。这是我做的一个实验 在看到你的问题后完成
#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.*/
}
#包括
使用名称空间std;
main()
{
int e[2]={0,1};
int*p[2];
int i;
/*
使用一个指针打印数组e内容
从一个指针数组。这里我不使用p[2]
完全
*/
p[1]=e;
如果这个答案完全不相关,我很抱歉,但是
我很想把它贴出来。这是我的一个实验
在看到你的问题后完成
#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.*/
}
#包括
使用名称空间std;
main()
{
int e[2]={0,1};
int*p[2];
int i;
/*
使用一个指针打印数组e内容
从一个指针数组。这里我不使用p[2]
完全
*/
p[1]=e;
如果这个答案完全不相关,我很抱歉,但是
我很想把它贴出来。这是我的一个实验
在看到你的问题后完成
#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.*/
}
#包括
使用名称空间std;
main()
{
int e[2]={0,1};
int*p[2];
int i;
/*
使用一个指针打印数组e内容
从一个指针数组。这里我不使用p[2]
完全
*/
p[1]=e;
如果这个答案完全不相关,我很抱歉,但是
我很想把它贴出来。这是我的一个实验
在看到你的问题后完成
#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.*/
}
#包括
使用名称空间std;
main()
{
int e[2]={0,1};
int*p[2];
int i;
/*
使用一个指针打印数组e内容
从一个指针数组。这里我不使用p[2]
完全
*/
p[1]=e;
CUT1。通过任何数组在C++中运行以改变内容:
请参阅此代码段:
//电话:
int-nArr[5]={1,2,3,4,5};
Mul(nArr,5);
每当你传递一个数组来运行时,实际上你会把指针传递给数组的第一个元素。这是C++和C.的隐式,如果你通过正常值(非数组),它会被认为是通过值。
//函数Mul()的声明和定义
void MUl(int*nArr,size\t narrize){
}
//复制后,nArrB指向5元素数组的第一个元素
<>希望有帮助。请进一步澄清。< /P > 1。将任何数组传递给C++中的函数以更改内容:
请参阅此代码段:
//电话:
int-nArr[5]={1,2,3,4,5};
Mul(nArr,5);
每当你传递一个数组来运行时,实际上你会把指针传递给数组的第一个元素。这是C++和C.的隐式,如果你通过正常值(非数组),它会被认为是通过值。
//函数Mul()的声明和定义
void MUl(int*nArr,size\t narrize){
}
//复制后,nArrB指向5元素数组的第一个元素
<>希望有帮助。请进一步澄清。< /P > 1。将任何数组传递给C++中的函数以更改内容:
请参阅此代码段:
//电话:
int-nArr[5]={1,2,3,4,5};
Mul(nArr,5);
每当你传递一个数组来运行时,实际上你会把指针传递给数组的第一个元素。这是C++和C.的隐式,如果你通过正常值(非数组),它会被认为是通过值。
//函数Mul()的声明和定义
void MUl(int*nArr,size\t narrize){
}
//复制后,nArrB指向