如何传递数组主FUNC。 W / C ++ [英] How to pass arrays in the main func. w/ c++

查看:114
本文介绍了如何传递数组主FUNC。 W / C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>

using namespace std;

const int MAX = 1000;
int  ArrMix[MAX];
int *ptrArrPos[MAX];
int *ptrArrNeg[MAX];
int PosCounter = 0;
int NegCounter = 0;
int r;


void accept(int ArrMix[MAX])
{
    cout<<"Enter the number of elements in your array: ";
    cin>>r;

    for (int i = 0; i < r; i++)
    {
        cout<<"Enter value:";
        cin>>ArrMix[i];
    }
}

void process(int &i)
{
    if(ArrMix[i] >= 0)
    {
        ptrArrPos[PosCounter] = &ArrMix[i];
        PosCounter++;
    }
    else
    {
        ptrArrNeg[NegCounter] = &ArrMix[i];
        NegCounter++;
    }
}

void display(int &i)
{
    cout<<"Your passed array is: " << endl; 
    cout<<ArrMix[i] << endl;
    cout <<"Total number of positive integers is: "<<PosCounter<<endl;
    cout<<"Your positive array is: "<<endl;
    for (int i = 0; i < PosCounter; i++)
    {
        cout << *ptrArrPos[i] << endl;
    }  
    cout<<endl;
    cout <<"Total number of Negative integers is: "<<NegCounter<<endl;
    cout<<"Your negative array is: "<<endl;
    for (int i = 0; i < NegCounter; i++)
    {
    cout << *ptrArrNeg[i] << endl;
    }
}

int main ()
{ 
    int *i;
    int a = &i;
    accept(&ArrMix[MAX]);
    process(a);
    display(a);

    system("pause>0");
}

在code你在上面看到的是一个程序使用,以创建数字的用户定义的数组列表。它应该从用户接受数字,显示传递的数组,正数阵列及其负数阵列。它应该评估的项目,这意味着分离底片从正数则创造了每一个阵列#秒。下是使用一个计数器来确定多少正#分别与负#分别各自数组中为止。
我有在传递数组从一个功能到另一个使用指针,把它的主要功能出现问题。所以请帮助我?

The code you see above is a program use to create a user-defined array list of numbers. It should accept numbers from the user, display the passed array, positive numbers array and its negative numbers array. it should evaluate items, meaning separating negatives #s from positive numbers then creating an array for each. next is to use a counter to identify how many positive #s and negative #s are in each array. I am having problems in passing the array from one function to another using pointers and calling it in the main function. so please help me?

推荐答案

这位前pression &放大器; ArrMix [MAX] 返回一个指向索引整数 MAX 的数组中为止。该指数是一个数组超越,这意味着你传递一个指针数组的功能,那么这会很乐意写入内存超越。

The expression &ArrMix[MAX] returns a pointer to the integer at index MAX in the array. This index is one beyond the array, meaning you pass a pointer to beyond the array to the function which will then happily write to that memory.

传递一个数组很简单,只要通过任何其他的说法:

Passing an array is as simple as passing any other argument:

accept(ArrMix);

您也有一个问题就在这里:

You also have a problem here:

int *i;
int a = &i;

您声明 I 是一个指向 INT 。然后,可以使用&放大器;我其中变量,换句话说,一个指针的地址返回一个指针一个 INT ,并尝试这双指针赋值给一个正常的 INT 变量。

You declare i to be a pointer to an int. Then you use &i which returns the address of the variable i, in other words a pointer to a pointer to an int, and try to assign this double-pointer to a normal int variable.

在我看来,你可能要返回条目的数量是在条目用户在输入访问函数,然后循环数组中阵列和呼叫过程的每个值。然后在显示,而不是采取应采取的规模和遍历了 ArrMix 指数显示 ArrMix 阵列。

It seems to me that you might want to return the number of entries is in the array from the access function, and then loop over the entries the user entered in the array and call process for each value. And then in display instead of taking the ArrMix index it should take the size and loop over that to display the ArrMix array.

这篇关于如何传递数组主FUNC。 W / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆