简体   繁体   中英

Comparing elements in the array by use of pointers (C++)

I am creating the function, compare_arrays to check if elements inside the pre-defined arrays are similar. I want to use pointer and pass the arrays by reference into the function. This is the function I coded so far but pylint is giving me an error for my second if loop stating that *arry1 and *arry2 are not defined. Can someone help me understand this error better.

Also, how do I call the function and pass in arr_one, arr_two, etc.....

#include <iostream>

using namespace std;

int size1 = 3, size2 = 3;
int arr_one[] = {1, 2, 3};
int arr_two[] = {1, 2, 3};



int main(){

    bool compare_arrays( int *arry1, int size1, int *arry2, int size2);{
        if (size1 != size2){
            return false;
        }
        for(int i=0; i < size1; i++);{
            if (*arry1 != *arry2);{
                return false;
            }
        }
        *arry1++;
        *arry2++;
        return true;
        }
}

The biggest and main problem in your code is the inclusion of the ; at the end of compare_arrays . What you're actually doing is declaring a function, compare_arrays , then creating a separate scope, which is why arry1 is not defined. If you remove this semi-colon, your error will start to make more sense:

bool compare_arrays( int *arry1, int size1, int *arry2, int size2) {

Now the compiler will tell you you cannot define an function within a function, which is easy to solve. Move compare_arrays outside of main :

bool compare_arrays( int *arry1, int size1, int *arry2, int size2){
    if (size1 != size2){
        return false;
    }
    for(int i=0; i < size1; i++);{
        if (*arry1 != *arry2);{
            return false;
        }
    }
    *arry1++;
    *arry2++;
     return true;
}

int main() {
}

This will now compile, but probably not run as you expect, because you have an extra erroneous semi-colon at the end of your second if statement. What this means is the semi-colon is treated as the expression from the if , then you'll always return false; . Fix this by removing the semi-colon:

for (int i = 0; i < size1; i++) {

And now you can simply call your code in main like so:

int main() {
    if (compare_arrays(arr_one, size1, arr_two, size2)) {
        std::cout << "arrays are equal\n";
    }
}

However, you can trivialise the whole thing by using std::vector :

#include <vector>
#include <iostream>
int main() {
    std::vector<int> v1 = { 1, 2, 3 };
    std::vector<int> v2 = { 1, 2, 3 };
    if (v1 == v2)
        std::cout << "arrays are equal\n";
}

You put compare_arrays function body inside the main function. remove the body of the function out the main and just call it from inside the main function. Also, you put the semi-colon after if statement and function header and that's wrong. your code should be like this

  #include <iostream>

using namespace std;

int size1 = 3, size2 = 3;
int arr_one[] = { 1, 2, 3 };
int arr_two[] = { 1, 2, 3 };

bool compare_arrays(int *arry1, int size1, int *arry2, int size2) {
    if (size1 != size2) {
        return false;
    }
    for (int i = 0; i < size1; i++) {
        if (*arry1 != *arry2) {
            return false;
        }
    }
    *arry1++;
    *arry2++;
    return true;
}

int main() {
    cout << compare_arrays(arr_one, size1, arr_two, size2) << endl;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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