Difference int*const a and const int *a

Hi, I have to create a funtion of a cumulative sum, in which the argument a is added to the argument b and c=a+b and I have three headings to choose from, explaining which of them would work and writing a program if it does.

a) void cumul(int *const a, int *const b, int*const c)
b) void cumul(const int *a, const int *b, const int *c)
c) void cumul (int &a, int &b, int &c)

but I have no idea of how to call a) and b) and if they would work, because they take const values! Thank you for your help!
Last edited on
I would prefer the third declaration though in my opinion the best declaration would be

int cumul( int a, int b );

Let consider your declarations in turn

void cumul(int *const a, int *const b, int*const c)

Here all three variables are pointers. You may not add one pointer to another. And moreover all three pointers are const.

void cumul(const int *a, const int *b, const int *c)

Here the objects to which the pointers refer can not be changed. But the ponters can be changed. However again you may nor add one pointer to another.


void cumul (int &a, int &b, int &c)

Here all three parameters are references. You can use this function for executing the expression c = a + b;
Thanks for the quick answer! I was thinking about that, the easiest way to do it would by with c), but is there a way of copying the pointers and changing the values for a) or b)? I would guess it would only work for b), because you said that the pointers could be changed
Well, I think there could be the following declarations

int cumul( int a, int b );

This is the best declaration. It is called as

int a = 10, b = 20;

int c = cumul( a, b );

Another declaration

int cumul( const int *a, const int *b );

It is called as

int a = 10, int b = 20;
int c = cumul( &a, &b );

Next declaration

int cumul( const int &a, const int &b );

It is called as

int a = 10, b = 20;

int c = cumul( a, b );

Declarations with return type void

void cumul( const int *a, const int *b, int *c );

It is called as

int a = 10, b = 20;

int c;

cumul( &a, &b, &c );

And at last

void cumul( const int &a, const int &b, int &c );

It is called as

int a = 10, b = 20;

int c;

cumul( a, b, c );
Topic archived. No new replies allowed.