passing whole 1d array to function by call by value(integer)

how can i demonstrate this thing I am very new to pointers and have just a bit of knowledge in it....

Till now I have attempted only the programs given in my book E.balaguruswami's
OOP with C++....

So please help me out
The short answer is: you can't.
The long answer is: why would you want to? Just pass a reference (a pointer):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int sum( int* xs, unsigned n )
  {
  int result = 0;
  while (n--)
    {
    result += xs[ n ];
    }
  return result;
  }

int main()
  {
  int numbers[] = { 7, 8, 9, 10, 11 };

  cout << "The sum is " << sum( numbers, 5 ) << ".\n";  // prints 45

  return 0;
  }

Hope this helps.

[edit] If you want to be specific about it, you can prototype your function arguments to indicate that the argument array will not be modified:
4
5
6
int sum( const int* xs, unsigned n )
  {
  ...
Last edited on
First, why do you want to pass an array by value? It can be done in other ways. You can make a struct that contains the array and pass the struct instance by value. Or you can use the std::vector instead of a c-array. Still, I am not sure why you would want to pass the array by value.
Topic archived. No new replies allowed.