You are passing to it an argument haveing type pointer to an array of 10 elements of type int. So the types of the parameter and of the argument differ.
What do you mean here. I mean i cannot understand. Are you saying i am passing by pointer in one place and on the other place i am passing by reference?
vlad from moscow (5999)
You are wrong. Arrays are not pointers. You were already shown an example
int a[10];
int *p = a;
std::cout << "sizeof( a ) = " << sizeof( a ) << std::endl;
std::cout << "sizeof( p ) = " << sizeof( p ) << std::endl;
If the array would be a pointer their sizes would be equal.
to answer this read this:
sizeof() : When applied to a reference type, the result is the size of the referenced type
and when applied to array gives the size of whole array in bytes becox it has not been referenced DIRECTLY but pointer does have. You can access every element of array just becox it's first element and it's ADDRESS in which you increment to get next elements.
@Computergeek01 Everyone or at least those who have been here a while: A post about pointers that gets the entire board riled up. Why does this sound familiar?
I don't have any idea about above posts. If such a post is discussed already then can you give me the link to that post please?
Doh, I did forget const. EDIT: I could have sworn there was some funky syntax that allowed you to use pointer syntax to initialize arrays... maybe that was an old GCC extension.
@ OP: I know, the ampersand is called a reference operator, it is not as you described an "address" operator which I'm pretty sure is a term you just made up for this argument. And to address your earlier post, the reason you need to use this is because you can not set a pointer equal to an actual datatype, you need to assign it to the address of that variable or in other words you need to reference it.
@ OP: I know, the ampersand is called a reference operator, it is not as you described an "address"operator which I'm pretty sure is a term you just made up for this argument. And to address your earlier post, the reason you need to use this is because you can not set a pointer equal to an actual datatype, you need to assign it to the address of that variable or in other words you need to reference it.
& as a address operator is common try following:
1 2 3 4 5
int *pointer=NULL;
int number=12;
pointer=&number;
cout<<"address of number is: "<<pointer;
cout<<"read number is: "<<*pointer;
And I do not know why you are showing this quote/ What does it prove?! I showed you already that results of sizeof for an array and a pointer are different
Moreover for an array you can calculate the number of its elements the following way
sizeof( array ) / sizeof( *array )
For any pointer this formula gives you an incorrect result.
& in that context is (what is often called) the address-of operator. I don't believe the standard refers to it as anything other than the unary & operator. The unary & operator results in the address of the variable it is applied to in the form of a pointer-to-variable-type. Fun fact: the unary & operator can be overloaded.
I notice the OP continues to fail to respond to my post which pointed out that operations legal on pointers are not legal on arrays (not that I expect him to) and continues to make (invalid) arguments about references which don't apply to pointers.
Arrays are not pointers. Pointers are not references.
An array decays into a pointer when passed to a function.
In the function it was defined, you could consider it very much like a const pointer, which the compiler can get size of the chunk of memory it points to. That is why you cannot do this,
1 2 3 4 5 6
int a[10];
++a;
//and you cannot do this for the same reason
int *const a;
++a;
sizeof is able to get you the size of the array in the same function it was declared in because it is calculated at compile time.
Also sizeof() is not a function, but an operator. If it were a function, then it could not possibly return the size of an array, because if you passed the array, it would be passed as a const pointer.
Then why do you have to assign it by reference in Line 7 of your code segment above?