In the book I am studying it says that if I pass a vector to a function, the name of the vector is always treated as a pointer. In fact it's so. But I can't understand why in the first function the const clause is allowed by the compiler, while in the second function (where I use pointers to search for the maximum value between the elements) no. In the functions I would simply like to protect against the modification of the vector.
#include <stdio.h>
int find_largest(const int a[], int n);
int find_largest_with_pointer(const int *vettore, int n);
int main(void) {
int my_number[] = {5, 7, 90, 34, 12};
int n = sizeof(my_number) / sizeof(my_number[0]);
int *pmy_number = my_number;
printf("%d\n", find_largest(my_number, n));
printf("%d\n", find_largest(pmy_number, n));
printf("%d\n", find_largest_with_pointer(my_number, n));
printf("%d\n", find_largest_with_pointer(pmy_number, n));
return 0;
}
int find_largest(const int a[], int n) {
int i, max;
max = a[0];
for(i = 0; i < n; i++)
if(a[i] > max)
max = a[i];
return max;
}
int find_largest_with_pointer(const int *vettore, int n) {
int *i, max = *vettore;
for(i = vettore; i < vettore + n; i++)
if(*i > max)
max = *i;
return max;
}