Rolf Magnus said:
Ok, I have to admit that char is not an integer type, but it is an
integral type. And signed char and unsigned char still are integer
types. Some quotes from the chapter 3.9.1 "Fundamental types" of the
C++ standard:
Glad to see your eventually starting to come to your senses
2 There are four signed integer types: "signed char", "short int",
"int", and "long int". ...
3 For each of the signed integer types, there exists a corresponding
(but different) unsigned integer type: "unsigned char", "unsigned
short int", "unsigned int", and "unsigned long int"...
...
7 Types bool, char, wchar_t, and the signed and unsigned integer
types are collectively called integral types. ...
Just because an array happens to be turned into a pointer by the
compiler when you pass it to a function deoesn't mean to say that
we're not passing an array to a function.
Yes it is. You can determine the size of an array, but if you pass a
pointer to its first element to a function, you will only get the
size of a pointer, not of the array, since a pointer was passed to
the function, not an array. Another difference is that passing a
value to a funciton means that this value is copied, and the function
works on a copy. This is not the case for arrays, since arrays are
not copyable. The funciton can access the original array through a
pointer, not a local copy.
Consider the following array:
int* Array = new int[128];
Do you suggest this is not an array since you cannot determine the
size of it?
Depends on what you refer to as 'this'. 'Array' is a pointer, and you
can determine its size. It would be 4 on my machine. The array that it
points to doesn't have a name, and therefore, its size cannot be
determined, since sizeof either needs a type or the name of a variable.
Have you ever tried to determine the size of that array, I mean by
experimenting with a real compiler? I would love to see how you can
find out that size.
Hang on ...
Determining the size.........
determining.......
Determining the size..........
ok got it!
It's err 128 x sizeof(int)
Do you want to know how I figured this out?
No. The array doesn't have an identifier.
The array is identified by some memory address , this memory address is
stored in a variable which we use to access the array.
Therefore the identifier for this array as far as we are concerned is.....
'Array'
Again: A pointer is _not_ an array. Therefore, what you pass cannot be a
pointer and an array at the same time. It can only be _one_.
I never said a pointer was an array.
I can pass an array to a function without any problem whether it wants to
consider itself as an array, a pointer, a memory location, or a
electronically charged piece of material, or even a cluster of charged
atoms. Or perhaps it is an object?
So I may have just said an array was all of these things but I never said a
pointer was an array. did I?
It can still be indexed like an array so how do you explain that if
it's not an array?
The indexing is a pointer operation. Writing:
ptr[4] = 5;
is just a more convenient way of writing:
*(ptr + 4) = 5;
i.e. writing 5 into the address that is 4 beyond the pointer 'ptr'. And
since you can write it the other way round, too:
*(4 + ptr) = 5;
You can do the same with the index operator:
4[ptr] = 5;
Yes but your making out as if the pointer method is normative when it's
quite the opposite.
The normal way is to use the subscript operators and yes I have documents
that expalin all this too
it goes something like this:
quote:
A postfix-expression followed by the subscript operator, [ ], specifies
array indexing. A subscript expression represents the value at the address
that is expression positions beyond postfix-expression when expressed as
Usually, the value represented by postfix-expression is a pointer value,
such as an array identifier, and expression is an integral value (including
enumerated types).
end quote.
Note the bit that states:
the value represented by postfix-expression is a pointer value, such as an
array identifier
I do wish you'd take note of this as it is from a good source.
Still, 4 is not an array, and ptr not an index into it. When you use the
index operator on an array, this array is first converted into a
pointer to its first element, so that the index operator actually works
on a pointer.
Passing by reference means passing a reference. If you pass a pointer,
you pass a pointer. Nothing else.
No. There is a refernece type in C++ and when you say passing by reference
this does not necesarilly mean passing by reference type.
by val
Where is it defined that passing to a function means *making a copy of
it* ?
Passing by value does. Passing an array by reference is possible, and
that indeed doesn't copy it. That would look e.g. like:
#include <iostream>
void myfunc(int (&arr)[100])
{
std::cout << sizeof(arr) // prints the size of the array
<< std::endl;
}
int main()
{
int arr[100];
myfunc(arr);
}
But this doesn't clarify where it stipulates that when you pass too a
function it must be by value.
Passing by value _is_ "simply passing". If you want to simply pass an
int, you do:
void myfunc(int i) // pass an int - by value
{
}
If you instead want to pass by reference, you have to explicitly say so:
void myfunc(int& i) // by reference, not "simply" by value
{
}
LOL This is nonsense. If you don't state one way or the other it doesn't
mean passing by value is to be assumed . Most of the time your working with
pointers and passing by reference anyway it's more common to specifically
stipulate when you want to pass value, now you bring it up.
Yes, and I showed above what passing an arary by reference looks like.
It's of limited use though, because the size is hard-coded in the
function.
It's actually quite usefull way of extracting the size of the array.
But you seem to be missing the point that passing by reference does not
simply mean passing by reference type, you can pass pointers or address too
which can also be considered 'passing by ref'
So do you agree that we can pass the object to a function yet?