Jumbo said:
Well that is very confusing.
On my system a char is 1 byte and an integer is 4 bytes.
So how is a char type an integer type?
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:
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. ...
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.
According to your idea there is no array here it's simply a pointer.
The pointer is the identifier for the array.
No. The array doesn't have an identifier.
This is the same when you pass an array to a function, the pointer to
the first index is passed as this is the identifier for the array.
You may prefer to call it a pointer and that's fine but your saying
that it's not an array and it is an 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_.
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;
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.
No there are two ways to pass
by ref (not meaning the reference type)
Passing by reference means passing a reference. If you pass a pointer,
you pass a pointer. Nothing else.
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);
}
Yeah well passing by value is a different thing from simply passing.
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
{
}
If I say I can't pass an object to a function...
I'm not saying I can't pass it by value but I can pass it by reference
am I?
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.
Not being able to pass an object is different from not being
able to make a copy of an object.
Yes.