Passing an array of chars to a function

J

Jumbo

Karl Heinz Buchegger said:
"Jumbo

Learn the language.
You cannot assign arrays. That's how it is since at least 20 years in C
and C++ didn't change it.

--
Learn to read.
He said you cannot assign to an array.
Would you like a demonstration of how to assign to an array?
int intarr[1];
intarr[0] =256;

I don't see what your problem is with this simple assignment.
Would you like a demonstration of how to declare and initialise a simple
variable first? Then you might be better prepared to move onto arrays. :)
 
J

Jumbo

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?
 
K

Karl Heinz Buchegger

"Jumbo
ok here's the array:
int* array = new int[128];

So what do you want to convert?

You mean something like this:
array = (int*) array;
????

I don't understand what you need to convert , I don't need to covnert
anything when I use arrays.
What are you talking about converting?

What has all this to do with calling a function. That's what
is the topic.
When an array is used as an argument to a function and pass
by value is used, the array decays into a pointer to it's first
element.

Since there are only 2 mechanisms to pass arguments to functions in C++
* pass by value
* pass by reference
and pass by reference it pretty useless, we are left with: In pass
by value you can't pass an array, since the array decays into a pointer
to it's first value. It is exactly this pointer that gets passed and
not the array itself.
Well I got somebody else arguing this point.
Seen as it wasn't my point in the first place and nothing to do with me.
perhapss you and Karl should take this up.

If you read carefully, we say the same thing:

Yes, as long as it behaves as if the array was not passed.
No, since you can not pass an array.
No I don't doubt anything I *know* I can pass an array to a function.

Then obviously you know more then the rest of the C++ community
(including the C++ standard and all compiler construction people).
A array cannot be passed per value.
So does this mean we can't pass any dynamically created objects to
functions?

No, who said that?
But what if I copied the memory address in another variable or even in a
file or something.

Then you have another pointer to that memory.
So strictly speaking: the allocated memory gets unaccessible when the
last pointer to that memory is lost. But that is nitpicking in a place
where nitpicking is absolutely unecesarry. Don't present yourself thumber
then you are.
Then you could destroy the whole program and I could still access the array.

If a tree falls in a wood and nobody is around, will it make a noise?

Arrays don't live in a file. The concept of arrays has a meaning
during program execution only.
So what if I did:
#include <iostream>

#define ARRAY128(a)(a=new char[128])
typedef char* ARRAYT;

int main()
{
ARRAYT arrayidentifier;
ARRAY128(arrayidentifier);
for(int i=0; i<128; ++i){
arrayidentifier = i;
std::cout<< i <<": "<< arrayidentifier<<" \t";
}
}

Is this an unnamed array of does this array have a name?


It is unnamed.

Then what shall we name it?
Do you have one of then books for expecting parents?
No, it can't. Using the preprocessor to hide the fact that ARRAYT is a
pointer won't change that.

What if I did this:
template<class ARRAY>
void foo(ARRAY arrayidentifier){
}

then passed arrayidentifier to this function. Am I not passing the array to
this function?


No you are not. No matter how often you repeat that, you cannot
pass arrays in C++ per value.
Then surely the pointer is the name or identifier for that particular area
of memory.

The pointer points to that memory. But the memory still has no name.
Otherwise we'de be going around calling everything that area of memory with
no name at offset blah blah blah.


It's you's who are saying array identifiers are pointers and not arrays,

Nobody said that.
Array identifiers decay under certain circumstances into pointers to
their first element, but they are *not* pointers.
don't try and turn the tables at this stage.
Were you going to quote something?

This was his quote, in case you missed it. And you clearly were
wrong. An array is not a pointer, a pointer is not an array.
 
R

Ron Natalie

Jumbo @uko2.co.uk> said:
He said you cannot assign to an array.
Would you like a demonstration of how to assign to an array?
int intarr[1];
intarr[0] =256;
intarr[0] is NOT an array. It is an int that happens to be in an array.
You can't assign arrays. They are the only data type in C++ that
doesn't have assignment semantics.
 
K

Karl Heinz Buchegger

"Jumbo
And we still pass an array to the function.

Not if you pass by value.
If an array is passed by value, it is always a pointer to the first
array element that gets passed.
That's not the same as passing an array.
What the compiler does with it does not alter the fact that we have passed
an array to a function.

It hasn't.
But the value of an array is exactly this so why would you need to convert
anything.
Have you ever dereferenced an array?

Yes. And so have you.

Whenever you write

array

you dereference the array. This is because the compiler immediatly
transforms the above to:

*(array + i)

And this presents another case, where an array decays into a pointer
to its first element.
I don't see where the conversion comes in. What are you converting?

int* anArray = new int[128]

Why do you need to convert this to pass it to a function?

I don't know. You brought up the dynamic allocation in that example.
If I pass anArray to a function, then it is very clear that I don't
pass an array, since anArray isn't an array (even if it is named that way).
anArray is a pointer, and of course I can pass a pointer per value to
a function.
I don't see why they're making a big fuss about passing arrays to functions.

Because it can't be done.
There's no need to convert anything.

There is the need to decay the array into a pointer to its first element.
But doing this is a fundamental different thing then what is used in all
other cases of pass by value. Part of passing something by value is that
a copy of what is passed is created, but this does not happen in the case
of arrays. Which is very clear if we remember, that arrays are never passed
by reference, but only a pointer to their first element (but of course
one gets a copy of the original pointer, as usual when pass by value
is specified).
Here's another example:
int* array = new int[500];

Do you suggest the identifier 'array' is not an array?

That's correct. 'array' is a pointer.

Are arrays on the heap not real arrays or something?

They are, but they are unnamed. They can only be referred to by a pointer.

So it's a real array but it's unnamed. hmmm
But what about the identifier 'array', isn't that it's name?

Yes. But 'array' in the above is a pointer. You wrote it to
be a pointer

No I wrote it to be an array.

Pardon me, but

int* array

clearly denotes a pointer!!!!!
What do with that pointer, what you assign to that pointer doesn't
matter. It is a pointer and it will stay a pointer until the variable
is detroyed.
It's an array
I cannot treat this array identifier as if it were a pointer to an int.
This identifier is an array identifier and it MUST be treated as such.

You are so clearly wrong.
I don't see where you get off on calling arrays pointers. We all know that
under the hood it points to the first element.
Once it has been allocated storage for an array it becomes an array
identifier.

Oh my.
That's all bullshit. Assigning something to a pointer doesn't
change the type of a variable.
Yes we don't need a lecture on array indexing.

You obviously need a lot of lectures.
So what if I took the value of that pointer( the address) would that be the
name of the array?

No. An address is not a name (at least not in the sense of C++).
But then I could say no variables are ever passed to functions, only either
copies of varaibles or pointers to varaibles are passed.

This time you are right. When we say: a variable is passed per value
to a function, we implicitely mean: a copy of that variables value
is passed to the function and stored in a fresh varible denoted
in the parameter list.
But since this is a little bit long winded, we shorten this to: 'the
variable gets passed' and everybody knows what it means.
Is that your attempt at explaining the difference between program memory
area and free store memory areas? :)

No it is the attempt (a very successfull) to visualize pointers to people
which obviously have no clue about what pointers do and how to work with
them.
 
K

Karl Heinz Buchegger

"Jumbo
Karl Heinz Buchegger said:
"Jumbo

Learn the language.
You cannot assign arrays. That's how it is since at least 20 years in C
and C++ didn't change it.

--
Learn to read.
He said you cannot assign to an array.
Would you like a demonstration of how to assign to an array?
int intarr[1];
intarr[0] =256;

You are not assigning to an array. You are assigning to an array element.
Thats a different thing.

Assigning to an array would be:

intarr = 5;

or

int jarray[1];

intarr = jarray;
I don't see what your problem is with this simple assignment.
Would you like a demonstration of how to declare and initialise a simple
variable first? Then you might be better prepared to move onto arrays. :)

Given your performance in this group I don't think you are in the
position to give lessons :)
 
R

Ron Natalie

Karl Heinz Buchegger said:
Not if you pass by value.
If an array is passed by value, it is always a pointer to the first
array element that gets passed.
That's not the same as passing an array.

There's not even a function type tha takes an array as an argument. The language
specifically adjusts any declaration of arrays as parameters to pointer. The conversion
of array to pointer gets called to match the function type BEFORE it is actually passed.
 
K

Karl Heinz Buchegger

"Jumbo
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?

Yes. But this time with tighter rules.
Since you claim it is possible to pass an array into a function,
do it in seperate function. This function can only use what
is passed to it and no, you are not allowed to pass the
arrays size.

In other words, fill in the body in the following function skelton
which determines the passed arrays size:

void foo( int arr[] )
{
cout << ..... <- Please fill in here
}
The array is identified by some memory address ,

That's not an identifier in the C++ sense.
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.

it *is* normative in the sense that it is the first thing a compiler
does when presented with an array subscript.
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:

Quote from where?
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.

Says who? (I mean: who says that this is a good source?)
All we care is the standards document. We don't need additional
good sources, cause all there is to say about C++ is written down
in the C++ standard.
 
R

Rolf Magnus

Jumbo said:
You should first get an understanding of what "convert" means in C++.
It means that you take an existing object and create from it a new
object that is another type. This doesn't neccesarily mean putting
all the data into the new object.

ok here's the array:
int* array = new int[128];

So what do you want to convert?

Nothing. new[] already returns a pointer.
You mean something like this:
array = (int*) array;
????

That's a cast, also called explicit conversion. If you want, you can
cast an array into a pointer, but if you don't it will be done
automatically where appropriate.
I don't understand what you need to convert , I don't need to covnert
anything when I use arrays.

Right, you don't convert explicitly. It's done implicitly for you.
Well I got somebody else arguing this point.
Seen as it wasn't my point in the first place and nothing to do with
me. perhapss you and Karl should take this up.

The implementation is allowed to internally pass the array, if it makes
it look like the array was not passed, though that seems pretty stupid,
because it would need a lot of extra work at no benefit. However, from
the programmer's point of view, that doesn't matter. For him, the array
was not passed to the function, whatever happened behind the scenes.
4.2 Array-to-pointer conversion
[conv.array]

1 An lvalue or rvalue of type "array of N T" or "array of unknown
bound
of T" can be converted to an rvalue of type "pointer to
T." The result is a pointer to the first element of the array.

It says an array *can* be blah blah ......

Right, and that's what you doubt above (and below, too).

No I don't doubt anything

You must have a bad short-term memory. Scroll up to the beginning of
this posting and read your own doubts.
I *know* I can pass an array to a function.

Then do so whenever you like. I am starting to suspect you are actually
just trolling, since you are repeatedly claiming things that got
disproven, even by quotes from the standard. You even got plonked by
respected and knowledgable regulars because you keep ignoring the
facts. Still you claim that you "*know*" different. And when you get
out of arguments, you are just starting to add new claims to discuss
about that are equally wrong.
oh right so it can't do what it likes at all then.
That's a condraction in terms.
It can do what it likes as long as what it likes is not one of the
things that's not allowed (condradiction in terms, doesn't make sense
to me)

You don't understand or you pretend to not understand. I won't discuss
it anymore.
Well it's not I never said it was. :)

Another contradicition. You can search the quote where you did that
yourself.
Here's another example:
int* array = new int[500];

Do you suggest the identifier 'array' is not an array?

That's correct. 'array' is a pointer.

Are arrays on the heap not real arrays or something?

They are, but they are unnamed. They can only be referred to by a
pointer.

So it's a real array but it's unnamed.

Yes, just like any other dynamically allocated object.

So does this mean we can't pass any dynamically created objects to
functions?

No, it doesn't mean that. The facts that an array cannot be copied and
that it's is often converted into a pointer to its first element are
not connected to the fact that dynamically allocated objects don't have
a name.
But what if I copied the memory address in another variable

Then what? What happened to the name of the array? Is the name of the
new variable now suddenly the name of the array?
or even in a file or something.

Is now the file name the name of the array?
Then you could destroy the whole program and I could still access the
array. if nothing had overwritten that area of memory it could still
contain the same values too.

That heavily depends on the OS. On most modern systems, this won't work.
So we could have cross program arrays passing the address via files.

This is indeed done sometimes in some operating systems that allow such
things, but it's not required by the standard that this works, and it
won't on most systems. I bet you never tried this, like you never tried
any example that others posted here that would disprove your point or
writing any examples that prove any of your claims.
Then what if I encoded the memory address into a character string then
it could be classified as the name of the array then no?

No, not in C++.
So what if I did:
#include <iostream>

#define ARRAY128(a)(a=new char[128])
typedef char* ARRAYT;

int main()
{
ARRAYT arrayidentifier;
ARRAY128(arrayidentifier);
for(int i=0; i<128; ++i){
arrayidentifier = i;
std::cout<< i <<": "<< arrayidentifier<<" \t";
}
}

Is this an unnamed array of does this array have a name?


It is unnamed.

Then what shall we name it?


We can't name an object dynamically. Either it has a name at compile
time or none at all. Yours is allocated dynamically, which in turn can
only mean that it doesn't have a name.
Do you have one of then books for expecting parents?


What if I did this:
template<class ARRAY>
void foo(ARRAY arrayidentifier){
}

then passed arrayidentifier to this function. Am I not passing the
array to this function?

Nope. Again, you can easily try it out by yourself. Use sizeof or
typeid() on 'arrayidentifier' and look what size and type it has.
Then surely the pointer is the name or identifier for that particular
area of memory.

The pointer _is_ not a name, it _has_ a name, and it _contains_ the
address of a memory location.
Otherwise we'de be going around calling everything that area of memory
with no name at offset blah blah blah.

I would be "going around calling" it "the object pointed to by the
pointer foo". If you access an object through a pointer, it doesn't
matter if that object has a name or not.
It's you's who are saying array identifiers are pointers

I'm saying the exact opposite. I'm saying pointers are not array
identifiers. Just scroll up a bit and you can see where you claimed
they are. I'll copy it here once again. You wrote: "Then surely the
pointer is the name or identifier for that particular area of memory."
and not arrays,

Array identifiers aren't arrays. They are the the names of arrays.
don't try and turn the tables at this stage.

Think about that again.
Were you going to quote something?

I did quote it. Just look at the below two lines. They were the quote I
was referring to:
I wrote what?

The quote below. When I write "you wrote:" that means that the following
text is the quote.

This is the quote:

And here, the quote ends.
 
G

Gary

Karl Heinz Buchegger said:
<<followed by all kind of nonsense and anti-nonsense>>

While this thread has been fun, I begin to worry about Jumbo.
Has anyone considered that his brain might have been taken over by the
spirit of Herbert Schildt?
 
D

Default User

"Jumbo
No I have just proved that arrays can be passed to functions.
Why would the assignment have been illegal , what's wrong with assigning a
character array to a character array?


You are either an idiot (as you don't believe the Standard to be
relevant) or a troll. Either way, not worth my time.


*plonk*




Brian Rodenborn
 
J

Jumbo

Default User said:
"Jumbo



You are either an idiot (as you don't believe the Standard to be
relevant) or a troll. Either way, not worth my time.


*plonk*




Brian Rodenborn

Good riddens Brian Rodenborn you idiot.
 
J

Jumbo

Karl Heinz Buchegger said:
"Jumbo

Not if you pass by value.
If an array is passed by value, it is always a pointer to the first

I thought you were arguing that arrays can't be passed to functions..
array element that gets passed.
That's not the same as passing an array.

What the compiler does with it does not alter the fact that we have passed
an array to a function.

It hasn't.
But the value of an array is exactly this so why would you need to convert
anything.
Have you ever dereferenced an array?

Yes. And so have you.

Whenever you write

array

you dereference the array. This is because the compiler immediatly
transforms the above to:

*(array + i)

And this presents another case, where an array decays into a pointer
to its first element.


So are you saying that an array identifier is the simply a pointer?
I don't see where the conversion comes in. What are you converting?

int* anArray = new int[128]

Why do you need to convert this to pass it to a function?

I don't know. You brought up the dynamic allocation in that example.
If I pass anArray to a function, then it is very clear that I don't
pass an array, since anArray isn't an array (even if it is named that way).
anArray is a pointer, and of course I can pass a pointer per value to
a function.

Again you seem to be saying that an array identifier is simply a pointer?
functions.

Because it can't be done.
But you said erlier:
quote:
If an array is passed by value, it is always a pointer to the first
end quote:

So you don't seem to be able to decide if an array can be passed or not.
There is the need to decay the array into a pointer to its first element.
But doing this is a fundamental different thing then what is used in all
other cases of pass by value. Part of passing something by value is that
a copy of what is passed is created, but this does not happen in the case
of arrays. Which is very clear if we remember, that arrays are never passed
by reference, but only a pointer to their first element (but of course
one gets a copy of the original pointer, as usual when pass by value
is specified).

what do you mean by the need to 'decay' the array?
Here's another example:
int* array = new int[500];

Do you suggest the identifier 'array' is not an array?

That's correct. 'array' is a pointer.

Are arrays on the heap not real arrays or something?

They are, but they are unnamed. They can only be referred to by a pointer.

So it's a real array but it's unnamed. hmmm
But what about the identifier 'array', isn't that it's name?

Yes. But 'array' in the above is a pointer. You wrote it to
be a pointer

No I wrote it to be an array.

Pardon me, but

int* array

clearly denotes a pointer!!!!!
What do with that pointer, what you assign to that pointer doesn't
matter. It is a pointer and it will stay a pointer until the variable
is detroyed.

This is what I wrote:
int* array=new int[128];
This is clearly denotes an array therefore it is an array identifier.
You are so clearly wrong. No I'm not.


Oh my.
That's all bullshit. Assigning something to a pointer doesn't
change the type of a variable.

Why is it bullshit? The concept of an array identifier is not bullshit.
You obviously need a lot of lectures. lol.


No. An address is not a name (at least not in the sense of C++).
So what is the name in C++ terms?
This time you are right. When we say: a variable is passed per value
to a function, we implicitely mean: a copy of that variables value
is passed to the function and stored in a fresh varible denoted
in the parameter list.
But since this is a little bit long winded, we shorten this to: 'the
variable gets passed' and everybody knows what it means.


I don't consider all C++ objects to be varaibles .
No it is the attempt (a very successfull) to visualize pointers to people
which obviously have no clue about what pointers do and how to work with
them.
I'm not talking about pointers I'm talking about passing arrays to functions
:)
 
J

Jumbo

Karl Heinz Buchegger said:
"Jumbo
Karl Heinz Buchegger said:
"Jumbo

Because arr1 is a pointer, you were able to assign a new value
to
it.
So you can't assign new values to an array?

No, you cannot.

Well if you really think this your obviously a bit of a crackpot.

Learn the language.
You cannot assign arrays. That's how it is since at least 20 years in C
and C++ didn't change it.

--
Learn to read.
He said you cannot assign to an array.
Would you like a demonstration of how to assign to an array?
int intarr[1];
intarr[0] =256;

You are not assigning to an array. You are assigning to an array element.
Thats a different thing.

Assigning to an array would be:

intarr = 5;

or

int jarray[1];

intarr = jarray;

Never heard so much nonsense in my life.
Of course your assigning to an array . An array IS the array elements. If
you alter the array eleemnts then you alter the array.
:)

Given your performance in this group I don't think you are in the
position to give lessons :)
performance?
lol
glad someone here is monitoring my perforamance , wonder what'll happen if I
don't perform well.?.
 
J

Jumbo

Karl Heinz Buchegger said:
"Jumbo
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?

Yes. But this time with tighter rules.
Since you claim it is possible to pass an array into a function,
do it in seperate function. This function can only use what
is passed to it and no, you are not allowed to pass the
arrays size.

In other words, fill in the body in the following function skelton
which determines the passed arrays size:

void foo( int arr[] )
{
cout << ..... <- Please fill in here
}
The size of the array is whatever you allocated it as.
The array is identified by some memory address ,

That's not an identifier in the C++ sense. So what is the identifier?
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.
it *is* normative in the sense that it is the first thing a compiler
does when presented with an array subscript.
It doesn't matter what the compiler does internally, the object is still an
array to you or I.
Quote from where?
Where does it matter , it's a good source and that's all you need to know.
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.

Says who? (I mean: who says that this is a good source?)
All we care is the standards document. We don't need additional
good sources, cause all there is to say about C++ is written down
in the C++ standard.
I say it's a good source, do you disagree with it?
You got a problem with what it says?
 
K

Karl Heinz Buchegger

"Jumbo
Here's another example:
int* array = new int[500];
[snip]
Pardon me, but

int* array

clearly denotes a pointer!!!!!
What do with that pointer, what you assign to that pointer doesn't
matter. It is a pointer and it will stay a pointer until the variable
is detroyed.

This is what I wrote:
int* array=new int[128];
This is clearly denotes an array therefore it is an array identifier.

Plonk.
There is no point in discussing with any further if you can't get
such simple things right.

A question:
int i = 3.0;
is i now a double?

Don't reply, I will not answer any more
 
J

Jumbo

Karl Heinz Buchegger said:
"Jumbo
Here's another example:
int* array = new int[500];
[snip]
No I wrote it to be an array.

Pardon me, but

int* array

clearly denotes a pointer!!!!!
What do with that pointer, what you assign to that pointer doesn't
matter. It is a pointer and it will stay a pointer until the variable
is detroyed.

This is what I wrote:
int* array=new int[128];
This is clearly denotes an array therefore it is an array identifier.

Plonk.
There is no point in discussing with any further if you can't get
such simple things right.

A question:
int i = 3.0;
is i now a double?

Don't reply, I will not answer any more
Thanks goodbness for that.
|You can't pass an array to a function huh? I never heard so much rubbish
in all my life.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top