计算c中的问题(视觉工作室)

时间:2021-09-30 00:37:35

this is the function code:

这是功能代码:

void statistics(int arr[], int n, int *positive, int *even, int *doubledigit)
{
    int i = 0, countP = 0, countE = 0, countD = 0;

    for(i = 0; i < n; i++)
    {
        if(arr[i] > 0)
            countP++;

        if((arr[i] % 2) == 0)
            countE++;

        if(abs(arr[i]) >= 10 && abs(arr[i]) < 100)
            countD++;
    }

    *positive = countP;
    *even = countE;
    *doubledigit = countD;
}

void main()
{
    //  double mat[size][size];
    int *positive = NULL, *even = NULL, *DoubleDigit = NULL;
    int arr4[] = {1, 3, 5, -45, 8, 8, 60, 800};
    int soa = sizeof(arr4);
    statistics(arr4, soa, &positive, &even, &DoubleDigit);
}

the problem is that the result of the even numbers is 28:

问题是偶数的结果是28:

why is it 28?? it should count the even numbers... http://i.stack.imgur.com/dS2us.png

为什么是28?它应该计算偶数...... http://i.stack.imgur.com/dS2us.png

4 个解决方案

#1


First the return type of main() should be int.

首先,main()的返回类型应为int。

Secondly for some reason you are passing the addresses of int pointers (that are initialised to NULL) to your function. Just pass int* parameters to your function like you should be.

其次由于某种原因,您将int指针的地址(初始化为NULL)传递给您的函数。只需将int *参数传递给您的函数就像您应该的那样。

Thirdly, sizeof returns the size of the array in bytes. You want to iterate over the number if elements in the array, not the byte count. Therefore you need to divide the byte count by the number of bytes in each element (sizeof(int)).

第三,sizeof以字节为单位返回数组的大小。您希望迭代数组中的数字,而不是字节数。因此,您需要将字节数除以每个元素中的字节数(sizeof(int))。

Try this instead

试试这个

int main()
{
  int positive =0, even = 0, DoubleDigit = 0;
  int arr4[] = { 1, 3, 5, -45, 8, 8, 60, 800 };
  int soa = sizeof(arr4)/sizeof(int);
  statistics(arr4, soa, &positive, &even, &DoubleDigit);
}

#2


Memory addresses of some value-buckets are stored in these:

某些值桶的内存地址存储在这些中:

int *positive = NULL, *even = NULL, *DoubleDigit = NULL;

You want to declare actual value-buckets where to store the results/values, not the storage for addresses, so change to:

您希望声明实际值桶存储结果/值的位置,而不是地址存储,因此请更改为:

int positive = 0, even = 0, DoubleDigit = 0;

Also, you want the number integers in arr4, so change to:

此外,您想要arr4中的数字整数,因此更改为:

int soa = sizeof(arr4) / sizeof(int);

#3


In your main() function: positive even DoubleDigit has been a pointer. However you pass their address to function statistics().

在你的main()函数中:正数甚至DoubleDigit一直是指针。但是,您将其地址传递给函数statistics()。

statistics(arr4, soa, &positive, &even, &DoubleDigit);

is equal to

等于

statistics(int arr[],int soa,int **positive, int **even, int **DoubleDigit);

but you declare it as

但你宣布它为

statistics(int arr[], int n, int *positive, int *even, int *doubledigit)

#4


Try the following

请尝试以下方法

void statistics( const int arr[], int n, int *positive, int *even, int *doubledigit )
{
    int i;

    for ( i = 0; i < n; i++ )
    {
        if ( arr[i] > 0 ) ++*positive;

        if ( arr[i] % 2 == 0 ) ++*even;

        if ( abs( arr[i] ) >= 10 && abs( arr[i] ) < 100 ) ++*doubledigit;
    }
}

int main( void )
{
    int positive = 0, even = 0, DoubleDigit = 0;
    int arr4[] = { 1, 3, 5, -45, 8, 8, 60, 800 };

    int soa = sizeof( arr4 ) / sizeof( *arr4 );
    statistics( arr4, soa, &positive, &even, &DoubleDigit );
}

As for your code then you declared pointers

至于你的代码,你就声明了指针

int *positive = NULL, *even = NULL, *DoubleDigit = NULL;

but they do not point to actual memory.

但他们并没有指出实际的记忆。

You call the function parsing addresses of the pointers themselves

您调用函数解析指针本身的地址

statistics(arr4, soa, &positive, &even, &DoubleDigit);

that is for example expression &positive has type int ** while the corresponding parameter of the function

例如,表达式&positive具有类型int **而函数的相应参数

void statistics( const int arr[], int n, int *positive, int *even, int *doubledigit )

is declared as having type int *

被声明为具有类型int *

Expression sizeof(arr4) yields the size in bytes of array arr4 while you have to pass the numjber of elements in the array.

表达式sizeof(arr4)产生数组arr4的字节大小,而您必须传递数组中元素的数量。

Function main shall have return type int

函数main应具有返回类型int

int main( void )
{
   //...
}

#1


First the return type of main() should be int.

首先,main()的返回类型应为int。

Secondly for some reason you are passing the addresses of int pointers (that are initialised to NULL) to your function. Just pass int* parameters to your function like you should be.

其次由于某种原因,您将int指针的地址(初始化为NULL)传递给您的函数。只需将int *参数传递给您的函数就像您应该的那样。

Thirdly, sizeof returns the size of the array in bytes. You want to iterate over the number if elements in the array, not the byte count. Therefore you need to divide the byte count by the number of bytes in each element (sizeof(int)).

第三,sizeof以字节为单位返回数组的大小。您希望迭代数组中的数字,而不是字节数。因此,您需要将字节数除以每个元素中的字节数(sizeof(int))。

Try this instead

试试这个

int main()
{
  int positive =0, even = 0, DoubleDigit = 0;
  int arr4[] = { 1, 3, 5, -45, 8, 8, 60, 800 };
  int soa = sizeof(arr4)/sizeof(int);
  statistics(arr4, soa, &positive, &even, &DoubleDigit);
}

#2


Memory addresses of some value-buckets are stored in these:

某些值桶的内存地址存储在这些中:

int *positive = NULL, *even = NULL, *DoubleDigit = NULL;

You want to declare actual value-buckets where to store the results/values, not the storage for addresses, so change to:

您希望声明实际值桶存储结果/值的位置,而不是地址存储,因此请更改为:

int positive = 0, even = 0, DoubleDigit = 0;

Also, you want the number integers in arr4, so change to:

此外,您想要arr4中的数字整数,因此更改为:

int soa = sizeof(arr4) / sizeof(int);

#3


In your main() function: positive even DoubleDigit has been a pointer. However you pass their address to function statistics().

在你的main()函数中:正数甚至DoubleDigit一直是指针。但是,您将其地址传递给函数statistics()。

statistics(arr4, soa, &positive, &even, &DoubleDigit);

is equal to

等于

statistics(int arr[],int soa,int **positive, int **even, int **DoubleDigit);

but you declare it as

但你宣布它为

statistics(int arr[], int n, int *positive, int *even, int *doubledigit)

#4


Try the following

请尝试以下方法

void statistics( const int arr[], int n, int *positive, int *even, int *doubledigit )
{
    int i;

    for ( i = 0; i < n; i++ )
    {
        if ( arr[i] > 0 ) ++*positive;

        if ( arr[i] % 2 == 0 ) ++*even;

        if ( abs( arr[i] ) >= 10 && abs( arr[i] ) < 100 ) ++*doubledigit;
    }
}

int main( void )
{
    int positive = 0, even = 0, DoubleDigit = 0;
    int arr4[] = { 1, 3, 5, -45, 8, 8, 60, 800 };

    int soa = sizeof( arr4 ) / sizeof( *arr4 );
    statistics( arr4, soa, &positive, &even, &DoubleDigit );
}

As for your code then you declared pointers

至于你的代码,你就声明了指针

int *positive = NULL, *even = NULL, *DoubleDigit = NULL;

but they do not point to actual memory.

但他们并没有指出实际的记忆。

You call the function parsing addresses of the pointers themselves

您调用函数解析指针本身的地址

statistics(arr4, soa, &positive, &even, &DoubleDigit);

that is for example expression &positive has type int ** while the corresponding parameter of the function

例如,表达式&positive具有类型int **而函数的相应参数

void statistics( const int arr[], int n, int *positive, int *even, int *doubledigit )

is declared as having type int *

被声明为具有类型int *

Expression sizeof(arr4) yields the size in bytes of array arr4 while you have to pass the numjber of elements in the array.

表达式sizeof(arr4)产生数组arr4的字节大小,而您必须传递数组中元素的数量。

Function main shall have return type int

函数main应具有返回类型int

int main( void )
{
   //...
}