What is the function of Pointer in this Example?

Correct( Add more things) me if I am wrong on this.So, I know pointer ( * ) points at objects/variable etc location. Location can be denoted by " & ". In this example, what is the job of the pointer and how is it being used to find the max of the stored arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 #include<iostream> 


using namespace std;


int *findMax(int arr[],int n);

int main(){
    int n,i,*p;
    cout<<"Enter number of data values";
    cin>>n;
    int arr[n];
    for(i=0;i<n;i++)     {
        cout<<"Enter value"<<i+1<<":";
        cin>>arr[i];
    }
    p=findMax(arr,n);
    cout<<"The max value is:"<<*p;
    
    return 0;
}

int *findMax(int data[],int n){
    int *max=data;
    int i;
    for(i=1;i<n;i++){
        if(*max<*(max+i)) *max=*(max+i);
    }     
    return max; 
}
It will help if you read up on pointers here: http://www.cplusplus.com/doc/tutorial/pointers/
Especially "Pointers and Arrays" section.

By the way, doing this:
1
2
3
   cout<<"Enter number of data values";
    cin>>n;
    int arr[n];

is a VERY bad idea. The compiler needs to know the size of your array at compile time.
Last edited on
@Arsian7041 Thank You! I will look into it. How can i fix the array issue. I can't put it up the cin and I can't think of anything other than that! Thank you again for pointing out! I will search online to see if they have something also.
The way to solve the array issue is to allocate memory using the new operator. If you havent learned about memory allocation, read up on it.
@Arsian7041 Thank You again!
By the way, the reason to avoid creating an array whose size is only known at run time is because it isn't standard C++. Thus a different compiler will probably fail to compile it.

As an alternative to allocating the array with new, you should consider using vector<> instead. One advantage of vector is that you don't have to worry about deallocating it.
if(*max<*(max+i)) *max=*(max+i);
This line is wrong. It should be
if(*max<*(data+i)) *max=*(data+i);
or better yet
if (*max < data[i]) *max = data[i];


Last edited on
@dhayden Ohh! Thank You! Just a quick question that goes out to anybody who sees this:

Why do we use pointers?
Is it because we want to save computer stress for not making copies of things rather just change it in it's original location.

As, I am learning this, i always find it easy to just assign a variable.
Arslan7041 wrote:
The way to solve the array issue is to allocate memory using the new operator.


Should avoid using new and delete, as dhayden says use STL containers instead. Otherwise investigate smart pointers.

sadij97 wrote:
Why do we use pointers? Is it because we want to save computer stress for not making copies of things rather just change it in it's original location.


Well that is the intention, and pointers are still used under the hood in libraries like the STL and others.

But for a user of C++, one can do a lot with out using pointers.

Already mentioned doing as much as one can with the STL, it takes care of memory and has a lot of handy features.

C++ also has references, which are like an alias for another variable, and behave in a similar way to pointers. A reference must be associated with a variable (but they can go out of scope) , whereas a pointer can be made to point at anything including nothing. So a reference is safer, and one should prefer to use this wherever possible.

As, I am learning this, i always find it easy to just assign a variable.


So use a reference if it is not a built-in type - int double etc.. That is, if it is a class - yours or an STL one like std::string, or std::vector say. Basically anything that could be big, pass by reference.


The other thing to realise here is that C++ is a federation of languages, including C, OOP C++ (classes), Template C++, and the STL.

Now what happens at school is that students write code that is barely C++ (uses iostream) but in reality is C code using raw pointers everywhere. Maybe they do this because they learnt C before C++. Also schools get their students to write their own versions of sort functions, linked lists, trees etc, so they get an understanding of how these things work. So this adds up to students not using the STL, or maybe being allowed to in a limited form depending on what the task is.

So we are at cross purposes here: on one hand it's good to get exposure to C coding; on the other, modern C++ is quite different.

Also, apparently there are a lot of teachers behind the times, they still teach new and delete for example. Maybe they see it as a more modern form of malloc and free. Maybe there is a lot of work in changing the syllabus to teach smart pointers instead.

On top of all that, there are lots of "old" examples of code on the internet.

> This line is wrong. It should be
> if(*max<*(data+i)) *max=*(data+i);
The line is indeed wrong, but you replace it for something equivalent (see line 25 int *max=data;)

The problem is that you are overwriting the first element of the array to put there the maximum.
Take a look at http://www.cplusplus.com/reference/algorithm/max_element/
1
2
if(*max < *(data+i))
   max = data+i; //note, no dereference 

Topic archived. No new replies allowed.