ARRAY..... Help????????????????

May 12, 2013 at 7:43pm
hello guys..

i know that an array is always passed by reference in C++
when we use it with functions.
but what if we use the reserved word const
for example

void num(const int w[])

does the array look like a value parameter now ?!?? !
i mean when i call the function num in the main

or what?!?!
Last edited on May 12, 2013 at 8:39pm
May 12, 2013 at 7:51pm
Arrays are not always passed by reference in C++.

When the name of an array is used as an argument to a function, it decays into a pointer to the first element of the array. This pointer is passed by value.

void num(const int w[])
is the same as
void num(const int* w)

Which is to say that w is a pointer to const int. The added const means: "This function will not modify anything pointed to by w."
May 12, 2013 at 7:52pm
The meaning of "const" in that context is that for the body of the function "num" the parameter w is an inmutable array of ints. In other words, the code of "num" cannot change the value of w. w is still a reference.

Being a reference or local copy is unrelated to whether variable is const or not.
May 12, 2013 at 7:52pm
@elmoro15
i know that an array is always passed by reference in C++


As opposed to you I do not know this. Arrays are passed by reference only if you explicitly specify a reference to an array. Otherwise arrays passed by values and adjusted to the pointer to their first element.

const means that elements of the array can not be changed.
May 12, 2013 at 8:06pm
!!!! ..
i know that array points to the location of its first element in the memory..

but i'm reading at my book that
arrays are passed by reference in C++;
suppose that i called a function the in the main
1
2
3
4
int main()
{
int num[10];
get(num);


1
2
void get(int z[])
suppose there are many statements here.


when i call get from the main .. any change that happens in the function get to the array Z ,, change the actual parameter which is the Array num
am I right????
now suppose that i put const in the formal parameter
so the function get becomes
void get(const int z[])
does any change happen to the array Z ,, change the actual parameter .. which is the array num /???????
May 12, 2013 at 8:19pm
const means read only
ex:
1
2
3
4
5
6
7
8
const std::string str("Giblit");
//can not be modified since it is read-only
std::string str2("Giblit");
//can be modified since it is not read-only
str2[0] -= 32;
std::cout << str << std::endl;
std::cout << str2 << std::endl;
//if you try to modify str you will get errors because it is const 
Last edited on May 12, 2013 at 8:20pm
May 12, 2013 at 10:46pm
This function

void get(int z[]);

id equivalent to

void get( int *p );

Inside the function p is a local variable and for example if you will write the following statement in the function

p = p + 1;

there will occur nothing with z.

To pass an array be reference means to declare the parameter the following way

void get( int ( &z )[10] );

The difference that in the first case when the array is passed by value sizeof( z ) will be equal to 4 on 32-bit system because z is adusted to the pointer while in the sacond case sizeof( z ) will be equal to 40 that is 10 * sizeof( int ). Moreover in the first case you should specify how many elements the array has. So the correct function declaration should be

void get( int z[], size_t n );

where n is the number of elements in the array.

It is not the array itself that is passed to the function in the first case. It is the pointer to the first element that is passed. Using the pointer you can cjhange elements of the original array.

When the function declared as

void get( const int z[] );

then it is equivalent to

void get( const int *p );

you can not change elements of the array because it is impossible to change memory content pointed by a const pointer.





Last edited on May 12, 2013 at 10:47pm
Topic archived. No new replies allowed.