Hello I'm having a problem with one of my programs the point of the program was to use pointers instead of array indexing to add all the integers of a particular array.
#include <iostream>
usingnamespace std;
int add(int *r)
{
int total;
for (int n = 0; n<4;++n)
{
total += *(r+n);
}
return total;
}
int main()
{
int trial[]={1,2,3,4};
int *p;
p = trial;
for (int n = 0; n<4;++n) // if removed the code suddenly bugs :|
{
cout << *(p+n) << endl;
}
cout << add(p) << endl;
system ("pause");
return 0;
}
The weird thing about this is without the code
1 2 3 4
for (int n = 0; n<4;++n)
{
cout << *(p+n) << endl;
}
It suddenly outputs the wrong number. :( I think I got the function right but I don't exactly know where the problem lies
@manasij7479, that's incorrect, please correct it. In pointer arithmetic, the size of the data pointed too has nothing to do with the size of a pointer.