An array is just a pointer

S

Stuart Golodetz

On 18/03/2011 13:44, Leigh Johnston wrote:
No; "pointing to an array of squash courts" is different to "a pointer
to an array of squash courts". This subtle difference is the cause of
your misunderstanding IMO.

/Leigh

The subtle difference is between the following two definitions:

1) For p to point to x, p must contain the address of x.

2) For p to point to x, p must not only contain the address of x, p must
also be of the correct type.

Consider:

#include <iostream>

int main()
{
int a[3];
int *p = &a[0]; // or just 'a'
int (*q)[3] = &a;
std::cout << p << ' ' << q << '\n';
return 0;
}

This prints out the same value twice -- &a[0] and &a have the same
*value*, but different *type*. By definition (1), both p and q point to
a. By definition (2), only q points to a, because p's type dictates that
it points to an int.

One can define "points to" either way (indeed any way one likes), but
definition (2) highlights that there is a difference between the way in
which p and q "point to" a, whereas definition (1) does not. For the
purposes of being precise when there may be ambiguity involved,
definition (2) thus appears preferable in more formal discussion. PMMV
(Paul's mileage may vary).

Unfortunately although your description seems to describe Paul's
misunderstanding perfectly he will still disagree due to the fact that
he is troll (in addition to being clueless).

/Leigh

That hadn't been entirely lost on me :) Just figured I'd at least
provide some clarity for the benefit of anyone else who might happen to
be reading the thread.

Cheers,
Stu

Note, incidentally, that I'm not saying that you can't say that "p
points to a", just that it's a colloquial usage of "points to" that
fails to fully capture the subtleties involved. In informal conversation
between people who understand the context, it may be an acceptable
colloquialism. For the purposes of e.g. standards documents, it's not.

Stu
 
P

Paul

Stuart Golodetz said:
On 18/03/2011 13:39, Paul wrote:
If you have a row of squash courts together like an array of squash
courts, which many of these places have.
[ ][ ][ ][ ][ ]

Stand outside these squashcourts and look at them as a group and point
to one of those courts with your finger. whilst standing there ask
yourself.. Am I pointing to the squash courts(plural) or the squash
court(singular)?

THe only reasonable answer is that you are pointing to both.

No; "pointing to an array of squash courts" is different to "a pointer
to an array of squash courts". This subtle difference is the cause of
your misunderstanding IMO.

/Leigh

The subtle difference is between the following two definitions:

1) For p to point to x, p must contain the address of x.

2) For p to point to x, p must not only contain the address of x, p must
also be of the correct type.

Consider:

#include <iostream>

int main()
{
int a[3];
int *p = &a[0]; // or just 'a'
int (*q)[3] = &a;
std::cout << p << ' ' << q << '\n';
return 0;
}

This prints out the same value twice -- &a[0] and &a have the same
*value*, but different *type*. By definition (1), both p and q point to a.
By definition (2), only q points to a, because p's type dictates that it
points to an int.

One can define "points to" either way (indeed any way one likes), but
definition (2) highlights that there is a difference between the way in
which p and q "point to" a, whereas definition (1) does not. For the
purposes of being precise when there may be ambiguity involved, definition
(2) thus appears preferable in more formal discussion. PMMV (Paul's
mileage may vary).

int* p = new int[16];

Creates a 1dim int array, and is identified by a int* type;

int(*)[16] would be the type of identifier for a 2dim array as follows:
int (*p2)[16] = new int[3][16];

A 2dim int array is identified by an int(*)[size] type.


Just accept this and move on before you become even more confused about
arrays and pointers and what they point to.
 
S

SG

int* p = new int[16];
Creates a 1dim int array, and is identified by a int* type;

int(*)[16] would be the type of identifier for a 2dim array as follows
int (*p2)[16] = new int[3][16];
A 2dim int array is identified by an int(*)[size] type.

This is the kind of terminology misuse that is indicative of your
incompetence. You may be able to write working C++ programs. But you
don't seem able to talk about C++ (and by that I mean the correct use
of well-established terminology).

A "type" never "identifies" an "array". Saying otherwise only makes
sense in *your* head. And instead of using the word "identifier" you
should have used the word "variable", at least in this context.
Instead of "is identified by" you should have said something along the
lines of "is accessable by". Instead of "by a T type" you should have
said "by a variable of type T" or "by an instance of T" or "by a T
instance". The use of "T type" to mean "T instance" or "instance of T"
is, again, misuse of the word "type".
Just accept this

There is no reason to prefer your vague and twisted terminology over
the C++ ISO standard's terminology.

SG
 
P

Paul

cg_chas said:
Stuart Golodetz said:
On 18/03/2011 13:44, Leigh Johnston wrote:
On 18/03/2011 13:39, Paul wrote:
If you have a row of squash courts together like an array of squash
courts, which many of these places have.
[ ][ ][ ][ ][ ]

Stand outside these squashcourts and look at them as a group and point
to one of those courts with your finger. whilst standing there ask
yourself.. Am I pointing to the squash courts(plural) or the squash
court(singular)?

THe only reasonable answer is that you are pointing to both.


No; "pointing to an array of squash courts" is different to "a pointer
to an array of squash courts". This subtle difference is the cause of
your misunderstanding IMO.

/Leigh

The subtle difference is between the following two definitions:

1) For p to point to x, p must contain the address of x.

2) For p to point to x, p must not only contain the address of x, p must
also be of the correct type.

Consider:

#include <iostream>

int main()
{
int a[3];
int *p = &a[0]; // or just 'a'
int (*q)[3] = &a;
std::cout << p << ' ' << q << '\n';
return 0;
}

This prints out the same value twice -- &a[0] and &a have the same
*value*, but different *type*. By definition (1), both p and q point to
a.
By definition (2), only q points to a, because p's type dictates that it
points to an int.

One can define "points to" either way (indeed any way one likes), but
definition (2) highlights that there is a difference between the way in
which p and q "point to" a, whereas definition (1) does not. For the
purposes of being precise when there may be ambiguity involved,
definition
(2) thus appears preferable in more formal discussion. PMMV (Paul's
mileage may vary).

int* p = new int[16];

Creates a 1dim int array, and is identified by a int* type;

int(*)[16] would be the type of identifier for a 2dim array as follows:
int (*p2)[16] = new int[3][16];

A 2dim int array is identified by an int(*)[size] type.


Just accept this and move on before you become even more confused about
arrays and pointers and what they point to.
Before you go any further I just had a quick scan over the jist of your post
and obvious its just big pile of disagreements an arguments that try to
stats my opinions as incorrect.
So lets see , what exactly is he saying and what exactly is he trying to say
is incorrect? He seems very unsure of himself.

Paul,

It is interesting that you use the word confusion to desribe the state
of mind of a person whose response to you was both logical and
correct. This in itself is quite revealing.
Blleeurg

Perhaps you should start listening to those with clear and pervasive
knowledge of the language before spouting off details that fail to
support your claim "that an array is just a pointer".

Bleeeurg.

you said,
A 2dim int array is identified by an int(*)[size] type.

Yes I did, its clearly stated above.
This is the same thing as saying that a pointer of array type points
to an array. i.e. which is just another assertion that Stuart's
definition (2) is entirely valid.

No its probably not the same as whatever that means in your mind.
I personally find humor in the fact that you've managed to assert
Stuart's case that a pointer of a given type points to the correct
type, in the same breath that you call him confused.
Blueeerg
This is why you're a mere troll with weak kung fu.

Regardless of how you choose to state the obvious, you fail to assert
your claim that an array is just a pointer.
Blueeerg

OTOH, I will demonstrate your incorrectness.
LOL Ok here we go , lets see firstly what exactly is he saying I am
incorrect about here? I don't know. ^_^.
Given pointer p2 whose type is int(*)[16] that has been initialized to
the address of the first element of the int[3][16] array, which in
this case is an element of array type, clearly itself is not the same
as the array itself. Correct
If the array was just a pointer, then the array
and pointer would share the same size.

No don't be stupid, why does the pointer need to be the same size as the
array?
int (*p2)[16] = new int[3][16];
std::cout << "p2 type is : " << typeid(p2).name() << std::endl;
std::cout << sizeof(int) << std::endl;
std::cout << sizeof(int) * 3 * 16 << std::endl;
std::cout << sizeof(p2) << std::endl;

output on my machine:
p2 type is : int (*)[16]
4
192
4
And what exactly is that piece of code suppoed to prove?
Some Blueerg about sizeof different types?
Further elaboration

What exactly have you elaborated on so far?
states that p2 can also be expressed an int[16]*
or a pointer to an array of 16 ints and that the pointer itself has no
awareness that storage for 16*3 ints were allocated.

Regardless of your desire to spin semantics, you can have pointers to
pointers, arrays of arrays, pointers to arrays, arrays of pointers,
and an infinite permutation of the former, but the pointer is not
itself an array any more than the array itself a pointer.
Bleurgh

You fight irrefutable logic with misguided abstraction, sir.
ok thanks for that , now you have a nice day :)
 
P

Paul

SG said:
int* p = new int[16];
Creates a 1dim int array, and is identified by a int* type;

int(*)[16] would be the type of identifier for a 2dim array as follows
int (*p2)[16] = new int[3][16];
A 2dim int array is identified by an int(*)[size] type.

This is the kind of terminology misuse that is indicative of your
incompetence. You may be able to write working C++ programs. But you
don't seem able to talk about C++ (and by that I mean the correct use
of well-established terminology).

Err hold on, all this type nonsense has been introduced by you(see comment)
.. I am simply trying to expalin how thinks work in your terms.
Comment: You or people on your side of the argument.

I know the type of the array is int, in this case, and thats all the type
info I need to know.


A "type" never "identifies" an "array". Saying otherwise only makes
sense in *your* head.

An array can be identified by a pointer of type int*.

int* p = new int[16];
p[0] = 0;

In the above expression the identifier 'p' is used as an identifier for the
array. In this case the identifier is also a type int*.
I think this is all pedantic language bullshit but this type of discussion
has not been introduced by me.

And instead of using the word "identifier" you
should have used the word "variable", at least in this context.
Instead of "is identified by" you should have said something along the
lines of "is accessable by". Instead of "by a T type" you should have
said "by a variable of type T" or "by an instance of T" or "by a T
instance". The use of "T type" to mean "T instance" or "instance of T"
is, again, misuse of the word "type".
As I said above the identifier "p" is a pointer type. I don't see what is
wrong with this statement.
int* p;

What type is "p"? It's a poiner type. Where is the confusion?
There is no reason to prefer your vague and twisted terminology over
the C++ ISO standard's terminology.
There is nothing vague and twisted about what I have said. It's quite normal
terminology.

Its similar to a Cat object:

class Animal{};
class Cat:public Animal{};

Animal* p = new Cat;
Here p points to a Cat object , but its type is Animal*.

In this case it can be said that p is a Cat because the pointer points to a
Cat. Its simply a shorthand way of saying the entity pointed to by p is a
Cat object.
Because its an Animal-pointer-type doesn't mean it can't point to a Cat
object.

Just google "passing an array to a function in C++" and you will see that ,
when a pointer represents an array, when such a pointer is dereferenced it
yields the arrays data. Therefore hundreds of programmers refer to such a
pointer as simply an array for example in the following code:

void foo(int* p){
for(int i=0; i<=10; ++i){ p =i;}
}

int* p = new int[11];
foo(p);

Inside this fucntion I can safely say that I have looped through the array.
Yes its obviously a pointer but it's also an array. :)
 
P

Paul

cg_chas said:
you said,
A 2dim int array is identified by an int(*)[size] type.

Yes I did, its clearly stated above.
This is the same thing as saying that a pointer of array type points
to an array. i.e. which is just another assertion that Stuart's
definition (2) is entirely valid.

No its probably not the same as whatever that means in your mind.

You should thank me for not criticising your choice of wording here,
after all, I am really only interested in seeing your proof of your
claim.

I was kind for giving you the benefit of the doubt as your use of the
"identified" has no place in a C++ discussion. A "quick scan" of the
standard perhaps was not enough for you.

Since however, you are disagreeing with the fact that a pointer of
array type points to an array of the proper type then I will say that
your statement above is somewhere between irrelevant and incorrect,
but in either case, not in any way assertive to your claim that "An
array is just a pointer"
Blueerg
OTOH, I will demonstrate your incorrectness.
LOL Ok here we go , lets see firstly what exactly is he saying I am
incorrect about here? I don't know. ^_^.
Given pointer p2 whose type is int(*)[16] that has been initialized to
the address of the first element of the int[3][16] array, which in
this case is an element of array type, clearly itself is not the same
as the array itself. Correct
If the array was just a pointer, then the array
and pointer would share the same size.

No don't be stupid, why does the pointer need to be the same size as the
array?
It is you that says that "An array is just a pointer".
I didn't say this. That is just the subject of the discussion.
But how do you come to the conclusion that this would mean a pointer to an
array is the same size as the array it points to?

If not in size then what characteristic of an array, in your mind,
gives it equality with a pointer?
I think the question here is what is going on in your mind to make you think
that a pointerto an array should be the same size as the array it points to?

You have yet to provide any coherent proof of this, yet others have in
fact disproved it over and over.

Proof of what?
Proof that a pointerto an array is not the same size as an array?

int* p_to_array = new int[16];
std::cout<< "sizeof pointer:\t"<<sizeof ( p_to_array)<< std::endl;
std::cout << "sizeof array:\t" <<sizeof(p_to array) * 16;

Is that good enough for you?
 
P

Paul

cg_chas said:
It is the subject of YOUR discussion and you have said it numerous
times. At least do the world the courtesy of owning your own
bullshit.

Please show me where I have said this.
The subject of the discussion is not a statement of fact, I did not state
whether I agreed with this or not.
I would certainly never say an array is *just* a pointer, although i do
understand the underlying mechanics of the C++ language that allows a
pointer to be used as an array.
I do not come to that conclusion. I only assert that the conclusion
must be true if the statement you made in the subject of this
discussion is true and it is not.
So you have come to this conclusion then :-S
see above..


proof that "An array is just a pointer". You can only attempt to deny
that you started the thread predicated on an incorrect statement, but
the fact remains, it is incorrect.
I have never said an array is *just* a pointer.
Proof that a pointerto an array is not the same size as an array?

int* p_to_array = new int[16];
std::cout<< "sizeof pointer:\t"<<sizeof ( p_to_array)<< std::endl;
std::cout << "sizeof array:\t" <<sizeof(p_to array) * 16;

Is that good enough for you?

Your endless trolling.. I alread proved the size difference. In fact
that proof invalidated the statement that is the subject of this
thread which is YOUR thread.

Your reading comprehension is as equally inept as your logic and
reasoning skills. Not that this surprises me, but I got the response
I needed in your response to SG. I responded there and refuted your
failing logic one and for all.

It was anticlimatic after all, but at least we got to the bottom of
your confusion.

Bluueerg
 
P

Paul

cg_chas said:
SG said:
On 18 Mrz., 17:41, Paul wrote:

int* p = new int[16];
Creates a 1dim int array, and is identified by a int* type;

int(*)[16] would be the type of identifier for a 2dim array as follows
int (*p2)[16] = new int[3][16];
A 2dim int array is identified by an int(*)[size] type.

This is the kind of terminology misuse that is indicative of your
incompetence. You may be able to write working C++ programs. But you
don't seem able to talk about C++ (and by that I mean the correct use
of well-established terminology).

Err hold on, all this type nonsense has been introduced by you(see
comment)
. I am simply trying to expalin how thinks work in your terms.
Instead of trying to guess what terms might be for understandable to
your reader in your explanations, perhaps do as SG suggests and stick
to terminology that is used in the standard.
An array can be identified by a pointer of type int*.
No, it cannot.
An array is not "identified " by a pointer.
Pointers do not "identify".

Says who?
If the array is pointed to by a pointer of array type then
dereferencing said pointer returns the array. Only the array that is
the type of the pointer is returned.

If the the array is pointed to by a pointer of int type then
dereferencing said pointer returns an int value. Only the int value
that is the type of pointer is returned.
int* p = new int[16];
p[0] = 0;

In the above expression the identifier 'p' is used as an identifier for
the
array. In this case the identifier is also a type int*.
I think this is all pedantic language bullshit but this type of discussion
has not been introduced by me.
Actually, of all the people that have taken the time to respond to
you, you're the only one making up bullshit terms instead of using
more well defined ones.
Just google "passing an array to a function in C++" and you will see that
,
when a pointer represents an array, when such a pointer is dereferenced it
yields the arrays data.
You seem to thrive on the ambiguity that a pointer "represents" or
"identifies" an array.

Only pointers of an array type point to an array of said type.
Only pointers of an array type return an array of data when
dereferenced.
Err no
A pointer of type int(*)[10] doesn't point to an array of type int[10], it
points to an array of type int[10];
You do not seem to understand that pointers to arrays point to an array of
n-1 dimensional array.
And pointers(of any type) do not return an array of data when they are
dereferenced.

8.3.4
"If the * operator, either explicitly or implicitly as a result of
subscripting, is applied to
this pointer, the result is the pointedto
(n - 1 )dimensional
array, which itself is immediately converted
into a pointer."

Please get your facts right before preaching about proper use of the
language terminology.


Therefore hundreds of programmers refer to such a Lol @ hundreds.
pointer as simply an array for example in the following code:

void foo(int* p){
for(int i=0; i<=10; ++i){ p =i;}
}

int* p = new int[11];
foo(p);

Inside this fucntion I can safely say that I have looped through the
array.

Yes you have looped through an array, one element at a time using a
pointer to a single element each iteration.
Yes its obviously a pointer but it's also an array. :)
It is ONLY a pointer, which was used a pointer to a single element
multiple times in a loop. In no way does this make it an array.


No its a pointerto an array.
Basically, we come to the conclusion now that your logic is as
follows.

Since we can pass a pointer to first element of an array to a function
and loop indexing the array with a pointer, then the pointer must be
an array?

Sorry Paul, this just is not correct.
Accessing arrays with pointers and operators does not make the arrays
themselves pointers.
Nobody is saying the array is a pointer.

I think you should hang your head in shame for a while, after this post, and
listen to someone who knows better instead of trying preach about things you
are not clear on.
After making the statements above ref:
" Only pointers of an array type point to an array of said type.
Only pointers of an array type return an array of data when
dereferenced"

You obviously are being misguided by the likes of Leigh Johnston and other
people who are twisting words around and declaring falsities.
 
P

Paul

cg_chas said:
your response to SG and i quote "its obviously a pointer but it's also
an array"
So please tell me where I stated that "an array is just a pointer".
So the world gets to guess which statements you make you are choosing
to stand by as factual and which are there for what? your trolling
entertainment?

Your entire debate has supported, albeit poorly, the statement in the
subject.
No it hasn't, my subject line is a bit of a joke TBH.
I think I have made my views clear about this and I would never say an array
is just a pointer.
I can only assume this is a troll response since it is clear that I
have not come to that conclusion. I stand by the data output that I
posted.

So did you come to a conclusion or not?
The comic value of you saying this is priceless.

You can say this until you are blue in the face, but the subject which
has your statement in it says otherwise.

No the subject is not a statemnt of my opinion. I am stating my opinion now
and its not up to you to decide.
 
P

Paul

Leigh Johnston said:
cg_chas said:
You have yet to provide any coherent proof of this, yet others have in
fact disproved it over and over.

Proof of what?
Proof that a pointerto an array is not the same size as an array?

int* p_to_array = new int[16];
std::cout<< "sizeof pointer:\t"<<sizeof ( p_to_array)<< std::endl;
std::cout << "sizeof array:\t" <<sizeof(p_to array) * 16;

Is that good enough for you?

Good enough? It is plain wrong; what you should have wrote was:

std::cout << "sizeof array:\t" <<sizeof(p_to_array[0]) * 16;
Oh look I forgot to dereference apointer, acceptable typo considering the
nonsensical argument I am having to put up with.
 
P

Paul

Leigh Johnston said:
On 18/03/2011 20:47, Paul wrote:
[...]
Err no
A pointer of type int(*)[10] doesn't point to an array of type int[10],
it points to an array of type int[10];


Wrong again; you don't understand pointers (or types) at all.


Why you are wrong:

int a1[10];
int (*p1)[10] = &a1; // type of 'p1' is pointer to an array of 10 ints
int a2[42][10];
int (*p2)[10] = &a2[0]; // type of 'p2' is also pointer to an array of 10
ints
int (*p3)[10] = new int[42][10]; // type of 'p3' is also pointer to an
array of 10 ints
// 'p1', 'p2' and 'p3' are all the *same* type namely a pointer to an
array of 10 ints

Why you are wrong :

8.3.4
"If the * operator, either explicitly or implicitly as a result of
subscripting, is applied to
this pointer, the result is the pointedto
(n - 1 )dimensional
array, which itself is immediately converted
into a pointer."


I think this concludes my case, and I hope this will end the nonsensical
arguements of these confused individuals :)
 
P

Paul

Leigh Johnston said:
On 18/03/2011 20:47, Paul wrote:
[...]
Err no
A pointer of type int(*)[10] doesn't point to an array of type int[10],
it points to an array of type int[10];


Wrong again; you don't understand pointers (or types) at all.


Why you are wrong:

int a1[10];
int (*p1)[10] = &a1; // type of 'p1' is pointer to an array of 10 ints
int a2[42][10];
int (*p2)[10] = &a2[0]; // type of 'p2' is also pointer to an array of 10
ints
int (*p3)[10] = new int[42][10]; // type of 'p3' is also pointer to an
array of 10 ints
// 'p1', 'p2' and 'p3' are all the *same* type namely a pointer to an
array of 10 ints

Why you are wrong :

8.3.4
"If the * operator, either explicitly or implicitly as a result of
subscripting, is applied to
this pointer, the result is the pointedto
(n - 1 )dimensional
array, which itself is immediately converted
into a pointer."


I think this concludes my case, and I hope this will end the nonsensical
arguements of these confused individuals :)
 
P

Paul

Leigh Johnston said:
Leigh Johnston said:
On 18/03/2011 20:56, Leigh Johnston wrote:
On 18/03/2011 20:47, Paul wrote:

[...]

Err no
A pointer of type int(*)[10] doesn't point to an array of type
int[10],
it points to an array of type int[10];

Wrong again; you don't understand pointers (or types) at all.


Why you are wrong:

int a1[10];
int (*p1)[10] = &a1; // type of 'p1' is pointer to an array of 10 ints
int a2[42][10];
int (*p2)[10] = &a2[0]; // type of 'p2' is also pointer to an array of
10 ints
int (*p3)[10] = new int[42][10]; // type of 'p3' is also pointer to an
array of 10 ints
// 'p1', 'p2' and 'p3' are all the *same* type namely a pointer to an
array of 10 ints

Why you are wrong :

8.3.4
"If the * operator, either explicitly or implicitly as a result of
subscripting, is applied to
this pointer, the result is the pointedto
(n - 1 )dimensional
array, which itself is immediately converted
into a pointer."


Why does that make me wrong? That part of the Standard you have quoted is
talking about what happens to the pointer resulting from an
"array-to-pointer conversion".

Why you are still wrong:

If the * operator is applied to p1, p2 or p3 above the result will be a
reference to an array of 10 ints; i.e. the following assertion holds:


"the result is the pointedto (n - 1 )dimensional array"

What part of this don't you understand?
The pointer points to an array of dim n, if you dereference it either
implciity or explicitily , the pointer points to an array of dim n-1.

Duh you just don't get it do you.
I will explain again:

int (*p)[10] = new int[3][10];
This pointer points an array of type int[3][10], that is a 2Dim array.
It doesn't point to an array of of type int[10].

The pointer *type* does not deifne the type of array it points to. The
pointer type is an n-1 dim type.

assert(sizeof(*p1) == sizeof(*p2) && sizeof(*p2) == sizeof(*p3) &&
sizeof(*p3) == sizeof(int[10]));
What is that bollocks about? ^^^^^^
Unfortunately nothing has been concluded as you still lack proper
understanding of the issue under discussion.
I understood this concept when I explained it to bigger people than you a
long time ago. At least they grasped the concept eventually, you don't seem
to be able to grasp it at all.
 
P

Paul

cg_chas said:
Says who?
Says anybody tired of seeing you use your own language semantics in
place of terms that have a strict meaning.
Only pointers of an array type point to an array of said type.
Only pointers of an array type return an array of data when
dereferenced.
Err no
A pointer of type int(*)[10] doesn't point to an array of type int[10],
it
points to an array of type int[10];

I said, "said type". You have changed my statement based on what is
going on in your head.

At least you are starting to use proper terminology, so progress has
been made.

A pointer of type int(*)[10], also the same as int[10]*, points to an
array of 10 int.

No you are wrong. it points to an array of type int[10].
When it's dereferenced it points to an array of int[10] type.

8.3.4
"If the * operator, either explicitly or implicitly as a result of
subscripting, is applied to
this pointer, the result is the pointedto
(n - 1 )dimensional
array, which itself is immediately converted
into a pointer."

<snip>
 
P

Paul

cg_chas said:
Duh you just don't get it do you.
I will explain again:

int (*p)[10] = new int[3][10];
This pointer points an array of type int[3][10], that is a 2Dim array.
It doesn't point to an array of of type int[10].

Absolutely incorrect.

p has only been initialized to the first element of the first array in
the array of int[3][10] and therefore can only point to the first
array of 10 ints.

p most certainly points to an array of 10 ints or int[10] and that is
all it points to.

The fact that new has allocated storage beyond p's type declaration is
irrelevant.
Learn the language properly kid.

8.3.4
"If the * operator, either explicitly or implicitly as a result of
subscripting, is applied to
this pointer, the result is the pointedto
(n - 1 )dimensional
array, which itself is immediately converted
into a pointer."
 
P

Paul

Leigh Johnston said:
Leigh Johnston said:
On 18/03/2011 21:23, Paul wrote:

On 18/03/2011 20:56, Leigh Johnston wrote:
On 18/03/2011 20:47, Paul wrote:

[...]

Err no
A pointer of type int(*)[10] doesn't point to an array of type
int[10],
it points to an array of type int[10];

Wrong again; you don't understand pointers (or types) at all.


Why you are wrong:

int a1[10];
int (*p1)[10] = &a1; // type of 'p1' is pointer to an array of 10 ints
int a2[42][10];
int (*p2)[10] = &a2[0]; // type of 'p2' is also pointer to an array of
10 ints
int (*p3)[10] = new int[42][10]; // type of 'p3' is also pointer to an
array of 10 ints
// 'p1', 'p2' and 'p3' are all the *same* type namely a pointer to an
array of 10 ints

Why you are wrong :

8.3.4
"If the * operator, either explicitly or implicitly as a result of
subscripting, is applied to
this pointer, the result is the pointedto
(n - 1 )dimensional
array, which itself is immediately converted
into a pointer."

Why does that make me wrong? That part of the Standard you have quoted
is talking about what happens to the pointer resulting from an
"array-to-pointer conversion".

Why you are still wrong:

If the * operator is applied to p1, p2 or p3 above the result will be
a reference to an array of 10 ints; i.e. the following assertion holds:


"the result is the pointedto (n - 1 )dimensional array"

What part of this don't you understand?
The pointer points to an array of dim n, if you dereference it either
implciity or explicitily , the pointer points to an array of dim n-1.


Again that part of the Standard is talking about what happens to the
pointer resulting from an "array-to-pointer conversion".

Dude work it out , I'm not here to teach people so ignorant and dumb as you
are. I've explained it enough times I'm not explaining it again
 
P

Paul

Leigh Johnston said:
cg_chas said:
An array is not "identified " by a pointer.
Pointers do not "identify".

Says who?
Says anybody tired of seeing you use your own language semantics in
place of terms that have a strict meaning.

Only pointers of an array type point to an array of said type.
Only pointers of an array type return an array of data when
dereferenced.

Err no
A pointer of type int(*)[10] doesn't point to an array of type
int[10], it
points to an array of type int[10];
I said, "said type". You have changed my statement based on what is
going on in your head.

At least you are starting to use proper terminology, so progress has
been made.

A pointer of type int(*)[10], also the same as int[10]*, points to an
array of 10 int.

No you are wrong. it points to an array of type int[10].
When it's dereferenced it points to an array of int[10] type.


When a pointer of type int(*)[10] is dereferenced the result is not a
pointer so it doesn't point to anything; the result of the dereference is
a reference of type int[10].


int (*x)[10] = new int[3][10];

If x is dereferenced like so ...x[0] , x is converted to a pointer to
int[10]. This means x is not a pointer to int[10] to begin with.
It's a pointer to int[3][10].
Work it out dummy.
 
P

Paul

cg_chas said:
cg_chas said:
Duh you just don't get it do you.
I will explain again:

int (*p)[10] = new int[3][10];
This pointer points an array of type int[3][10], that is a 2Dim array.
It doesn't point to an array of of type int[10].

Absolutely incorrect.

p has only been initialized to the first element of the first array in
the array of int[3][10] and therefore can only point to the first
array of 10 ints.

p most certainly points to an array of 10 ints or int[10] and that is
all it points to.

The fact that new has allocated storage beyond p's type declaration is
irrelevant.
Learn the language properly kid.

I was born in 1970 and started programming in 1986. If that makes me a
kid, then I'll take it.

As far as your repeatedly quoting the section of the stardard that
gives a rule for indexing multidimensional arrays, feel free to quote
all you want. You are however, misquoting and our out of context.

The section you keep incessantly pasting refers a rule for indexing
n-1 dimensional arrays.

The code above and that is in the context of this discussion is
initializing a pointer of int[10] type to a single array because that
is the type we have declared.

No conversion is necessary. We declared and allowed new to initialize
what part of the allocated space we are going to point to.

p is our pointer and its being initialized to the first element of the
first array only. p has no knowledge of any other arrays.

OTOH new certainly did when it allocated int[3][10], but p does not.
8.3.4
"If the * operator, either explicitly or implicitly as a result of
subscripting, is applied to
this pointer, the result is the pointedto
(n - 1 )dimensional
array, which itself is immediately converted
into a pointer."
You obviously don't understand this , I can't help you any further is you
refuse to listen.
GL
 
P

Paul

Leigh Johnston said:
Leigh Johnston said:
On 18/03/2011 22:12, Paul wrote:


An array is not "identified " by a pointer.
Pointers do not "identify".

Says who?
Says anybody tired of seeing you use your own language semantics in
place of terms that have a strict meaning.

Only pointers of an array type point to an array of said type.
Only pointers of an array type return an array of data when
dereferenced.

Err no
A pointer of type int(*)[10] doesn't point to an array of type
int[10], it
points to an array of type int[10];
I said, "said type". You have changed my statement based on what is
going on in your head.

At least you are starting to use proper terminology, so progress has
been made.

A pointer of type int(*)[10], also the same as int[10]*, points to an
array of 10 int.

No you are wrong. it points to an array of type int[10].
When it's dereferenced it points to an array of int[10] type.

When a pointer of type int(*)[10] is dereferenced the result is not a
pointer so it doesn't point to anything; the result of the dereference
is a reference of type int[10].


int (*x)[10] = new int[3][10];

If x is dereferenced like so ...x[0] , x is converted to a pointer to
int[10]. This means x is not a pointer to int[10] to begin with.
It's a pointer to int[3][10].


Wrong; x is not converted to a pointer to int[10] as it is *already* a
pointer to int[10]; x[0] is a reference to an int[10] array as
sizeof(x[0]) will confirm.

int x[3][5];

Here x is a 3 × 5 array of ints; more precisely, x is an array of three
element objects, each of which is an array of five ints. In the expression
x, which is equivalent to (*((x)+(i))), x is first converted to a pointer
to the initial array of five ints.

It cannot be any clearer than that.

GL
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top