Re: ISO standards
Jeffrey Baker wrote:
Here is a small counter-example:
#include <iostream>
using namespace std;
int main()
{
int array[] = {1,2,3,4,5,6,7,8,9,0};
int *ptr = array;
cout << "The value of ptr = " << ptr << endl;
cout << "The address of ptr = " << &ptr << endl;
The address here is where the pointer is "int *ptr = array".
cout << "The 'value' of array = " << array << endl;
cout << "The address of array = " << &array << endl;
cout << "The address of array[0] = " << &array[0] << endl;
return 0;
}
If an array were the same as a pointer, you would see at least two
different values being printed for the array lines, like it is for the
pointer lines.
I think you are referring to the pointer, ptr having its own address which
is
in simile with array.
I'm not quite sure what you mean by the above, but ptr has its own
address because it's a variable, whereas array is not. That is,
ptr = array; // ok
array = ptr; // not ok
Here is a counter:
#include <iostream>
using namespace std;
int main()
{
int array[] = {1,2,3,4,5,6,7,8,9,0};
int *ptr = array;
cout << "The value of ptr = " << ptr << endl; // address of the
where
the value is stored.
cout << "The address of ptr = " << &ptr << endl;
cout << "The 'value' of array = " << array << endl;
cout << "The address of array = " << &array << endl;
cout << "The address of array[0] = " << &array[0] << endl;
cout << "++ptr; " << endl;
++ptr;
cout << "The address of ptr = " << &ptr << endl; // address is the
same as
above.
cout << "The value of the ptr = " << ptr << endl;
cout << "The address of array[1] = " << &array[1] << endl;
return 0;
}
The address of &ptr is the same after ++ptr increments; like 'int array'
that points to the first element stays the same.
array can't be incremented either.
array++; // not ok
Maybe try this snippet and see if array and ptr are the same or different:
std::cout << sizeof(array) << std::endl;
std::cout << sizeof(array[0]) << std::endl;
std::cout << sizeof(ptr) << std::endl;
std::cout << sizeof(ptr[0]) << std::endl;
The value of the ptr matches the addess of the array[1].
These are both the address in which the value of array[1] is located.
The array is a collection of addresses that are pointed to.
I'm not quite sure that that means. When I think of collection of
addresses, I think of something like:
int a,b,c;
std::set<int*> s;
s.insert(&a);
s.insert(&b);
s.insert(&c);
I think of arrays as being a contiguous section of memory containing
objects. But that's not very formal.
The array is a higher form of the language.
In what sense of higher?
Suppose I write,
int *ptr = array;
1[ptr] = 6;
ptr[2] = 7;
Is that higher level?
They are both used differently.
They sure are. It would be very unlikely that you could do this
usefully with ptr instead of array,
for(unsigned int i=0; i<sizeof(array)/sizeof(array[0]); i++) {
std::cout << array[i] << std::endl;
}
They are paradoxes in the language. Not a conflict.
I'm not clear on what the paradox you refer to is.
I think the C faq might be useful on this issue. C and C++ are not the
same language, of course, but they are closely related. You might want
to start here, http://c-faq.com/aryptr/aryptrequiv.html and here
http://c-faq.com/aryptr/aryptrparam.html
HTH.
LR
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]