出了毛病,传递指针的函数 [英] Something went wrong with passing pointer to a function

查看:91
本文介绍了出了毛病,传递指针的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <cstdlib>
#include <iostream>
#include <Math.h>


using namespace std;

int stepCount,i,x,y,z,j,array1Size,array2Size,tester;
int numstring[10] = {0,1,2,3,4,5,6,7,8,9};
int numstringTest[10] = {0,1,2,3,4,5,6,7,7,9};
int* numbers;
int* differentNumbers;

void stepCounter(int a){

    // determines the step number of the number
    if(a/10 == 0)
           stepCount = 1;
    else if(a/100 == 0)
           stepCount = 2;
    else if(a/1000 == 0)
           stepCount = 3;
    else if(a/10000 == 0)
           stepCount = 4;
    else if(a/100000 == 0)
           stepCount = 5;
    else if(a/1000000 == 0)
           stepCount = 6;
    else if(a/10000000 == 0)
           stepCount = 7;
    else if(a/100000000 == 0)
           stepCount = 8;
    else if(a/1000000000 == 0)
           stepCount = 9;

}
void stepIndicator(int b){  
  // indicates each step of the number and pass them into array 'number'
  stepCounter(b);
  numbers = new int [stepCount];

  for(i=stepCount; i>0; i--){
     //
     /*
     x = (round(pow(10,stepCount+1-i)));
     y = (round(pow(10,stepCount-i)));
     z = (round(pow(10,stepCount-i)));
     */
     x = (int)(pow(10,stepCount+1-i)+0.5);
     y = (int)(pow(10,stepCount-i)+0.5);
     numbers[i-1] = (b%x - b%y)/y;
     }
  }


int sameNumberCheck(int *array, int arraySize){
   //checks if the array has two or more of same integer inside return 1 if same numbers exist, 0 if not
   for(i=0; i<arraySize-1; i++){
      //
      for(j = i+1; j<arraySize; j++){
        //
        if(array[i]==array[j]){
                              //
                              return 1;
                              }
        }

      }
      return 0;
}


void sameNumberCheckOfTwoArrays(int* array1, int* array2){

     //
     array1Size = sizeof(array1)/sizeof(array1[0]);
     array2Size = sizeof(array2)/sizeof(array2[0]);
     cout << array1Size << endl;

 }

int main(int argc, char *argv[])
{
    stepCounter(999999);
    cout << stepCount << endl;

    stepIndicator(826424563);
    for(j=0; j<9; j++){
       //
       cout << numbers[j] << endl;
    }
    cout << sameNumberCheck(numstringTest, 10) << " must be 1" << endl;
    cout << sameNumberCheck(numstring, 10) << " must be 0" << endl;
    //cout << sameNumberCheckOfTwoArrays(numstring, numstringTest) << " must be 10" << endl;
    sameNumberCheckOfTwoArrays(numstring, numstringTest);
    tester = sizeof(numstringTest)/sizeof(numstringTest[0]);
    cout << tester << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

我的code是像上面但我的问题很简单。你一定见过的功能sameNumberCheckOfTwoArrays。需为整数的指针(在本方案数组),发现数组的大小。方法很简单:

My code is like above but my question is very simple. You must have seen the function sameNumberCheckOfTwoArrays. takes to integer pointers(in this programme arrays) and finds size of the array. Method is simple:

array1Size = sizeof(array1)/sizeof(array1[0]);
array2Size = sizeof(array2)/sizeof(array2[0]);
cout << array1Size << endl; 

正如你所看到的。但是,当我调用该函数与numstring和numstringTest,每个有10个元素,它计算ARRAYSIZE 1?您可以执行code。但是,当我计算不使用funtion,你可以在code的底部看到,我得到10正常。这究竟是为什么?我想我通话功能和传球值代入函数吧?还是我没有?

As you can see. But when I call the function with numstring and numstringTest which each has 10 elements, it calculates the arraysize 1?! You can execute the code. But when I calculate without using funtion as you can see at the bottom of the code, I get 10 correctly. Why is this happening? I think I call function and pass values into function right? Or do I not?

推荐答案

您传递阵列sameNumberCheckOfTwoArrays为指针。在此过程中,阵列性被丢弃。你的函数只看到一个int *,因此报告的大小。

You are passing the arrays to sameNumberCheckOfTwoArrays as pointers. In this process, the "array-ness" is dropped. Your function only sees an int* and reports the size accordingly.

这篇关于出了毛病,传递指针的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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