简体   繁体   English

通过使用C ++中的指针查找偶数

[英]Finding even numbers by using pointers in c++

I am doing an assignment about pointers. 我正在做有关指针的作业。 In one of the question, it asks me to find even numbers in an array and print all of them. 在一个问题中,它要求我在数组中找到偶数并打印所有数字。 I also have to use the signature that is given by the assignment and I can not use use the & operator or [] notation in my function. 我还必须使用赋值给定的签名,并且不能在函数中使用&运算符或[]表示法。

Signature: void print_evens(int *nums,int length) ; 签名: void print_evens(int *nums,int length) ;

I know I have to use, 我知道我必须用

if(i%2==0)
  cout << i << endl;

to find the even numbers but I don't know how to do it with pointers. 查找偶数,但我不知道如何使用指针。

How can I pass an array from main function to the print_evens since there are no parameters for array? 由于没有数组参数,如何将数组从主函数传递给print_evens

Thank you for any help. 感谢您的任何帮助。

How can I pass an array from main function to the print_evens since there are no parameters for array? 由于没有数组参数,如何将数组从主函数传递给print_evens?

You can never pass an array to a function, regardless of the signature. 无论签名如何,都不能将数组传递给函数。 Arrays will decay to pointers to their first element in such a situation. 在这种情况下,数组将衰减到指向其第一个元素的指针。

In any case, you need to dereference the pointer to get the value at the current location. 无论如何,您都需要取消引用指针以获取当前位置的值。

for (int i = 0; i < length; ++i) {
    int current = *(nums + i);
    if ((current % 2) == 0)
        cout << current << endl;
}

And you can pass in your array (which decays to a pointer of course) 您可以传入数组(当然,它会衰减为指针)

#define len 10

int main(void) {
    int arr[len];
    /* initialize elements of arr */    
    print_evens(arr, len);
}

Consider the following code : 考虑以下代码:

void print_evens(int *nums,int length){

    for(int i = 0 ; i<length ; i++){
        int currNum = *(nums+i);
        if(currNum%2 != 1)
            cout<<currNum<<endl;    
    }
}

it will do what you want. 它会做你想要的。

But how it is done is it because nums is a pointer so you could simply iterate through it by adding number to it. 但是它是如何实现的,因为nums是一个指针,因此您可以通过向其添加数字来简单地对其进行迭代。 Every time you think you are working with array, in fact it is pointer to the array that you works with. 实际上,每次您认为要使用数组时,实际上都是指向您要使用的数组的指针。

according to the standard we have : 根据标准,我们有:

6.5.2.1 Array subscripting 6.5.2.1数组下标

A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. 后缀表达式后跟方括号[]的表达式是数组对象元素的下标名称。 The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). 下标运算符[]的定义是E1 [E2]与(*((E1)+(E2)))相同。

so even in arrays when you say nums[i] it means *(nums+i), and with using this fact you could simply work with pointers. 因此,即使在数组中当您说nums [i]时,它也意味着*(nums + i),并且使用此事实,您可以简单地使用指针。

void print_evens(int *nums,int length)
{
   for (int i = 0; i < length; ++i) {
      if ((nums[i] % 2) == 0)
        cout<< nums[i] << endl;
   }
}

int main(void) {
    int arr[<size of array you want>];
    //Assign values to array    
    print_evens(arr, len);
}

You can use pointers as arrays for your convenience. 为了方便起见,可以将指针用作数组。 No one will stop you :-) 没有人会阻止你:-)

Here is a demonstrative program 这是一个示范节目

#include <iostream>

void print_evens( int *nums, int length )
{
    for ( int *p = nums; p != nums + length; ++p )
    {
        if ( *p % 2 == 0 ) std::cout << *p << ' ';
    }
}

int main() 
{
    const int N = 10;
    int nums[N] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    print_evens( nums, N );

    std::endl( std::cout );

    return 0;
}

The output is 输出是

2 4 6 8 10 

Of course it would bebetter to declare the function like 当然,最好将函数声明为

void print_evens( const int *nums, size_t length );

that constant arrays also could be outputed. 常量数组也可以输出。

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

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