473,396 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

question regarding pointers

The question is about C++ (since its the C family, i posted it on this
newsgroup)

Lets say i declare an array
int a[] = {1,2,3,4};
int *p = a; //This is allowed because "a" returns the address of the first
element

The question is actually about 2D arrays. As far as I know, when a 2D array
is declared in C++, in the memory it is actually stored as a 1D array. and
the name of the array is pointing to the first element ie. a[0][0] i.e

int a[2][2] = { {1,2},{3,4} };

Now the question is that why cant i do this
int *p = a; //Compiler gives error. Whats wrong with this????? a is not an
int ** right?

However following are correct
int *p = &a[0];
int *p = &a[0][0];

Why doesnt int* p = a; work? (if you cout<<a<<&a[0]<<&a[0][0]; it gives the
same address)

Thanx in advance
Nov 15 '05 #1
5 1436
>The question is about C++ (since its the C family, i posted it on this
newsgroup)


I suggest you repost in microsoft.public.dotnet.languages.vc

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
100
Hi Wajih-ur-Rehman,

"> The question is actually about 2D arrays. As far as I know, when a 2D
array
is declared in C++, in the memory it is actually stored as a 1D array. and
the name of the array is pointing to the first element ie. a[0][0] i.e

int a[2][2] = { {1,2},{3,4} };

Now the question is that why cant i do this
int *p = a; //Compiler gives error. Whats wrong with this????? a is not an
int ** right?
No it is not.
Actually multidimensional arrays are treated differently when they has been
defined statically. They are array of arrays of arrays......
But it is not int** because no pointers are saved so it is not int **.

So when you do a[0] it returns one-dimensional array for the first row,
which can be cast to int *
However following are correct
int *p = &a[0]; This will fail (check it). a[0] is onedimensional array and it is already
pointer to the first element.
So what a[0] returns is address (number, constant) you cannot get the
address of a number. You can get address only of *l-values* (varaibles)
the correct is:
int *p = a[0];

Unlike a[0], which is not a *l-value* a[0][0] is a normal *l-value* and you
can get its address. So the following succeed. int *p = &a[0][0]; This is ok. a[0][0] returns a *l-value* of the first element and &a[0][0]
returns its address

Why doesnt int* p = a; work? (if you cout<<a<<&a[0]<<&a[0][0]; it gives the same address)


It shouldn't work. you cannot get &a[0]. However VC++ compiles it and just
change the type of the returning address. now it is of type int (*)[2] which
means pointer that points to array of 2 int values.
I don't know if it is by C/C++ spec and frankly I don't believe it.
Sometimes designers of the compilers simplify the grammer in order to make
it simple and the compiler faster. This simplifications lead to some really
rear erronous constructs to go thru and compiler may not report error at all
or may report not correct error.

So I believe:
int **b = &a[0] ;
should be reported as the error "'&' requires l-value";

Indexing operator [] works differently for multidimensional arrays when they
are declared statically and dynamically:
int a[2][2]
or
int **a;

And here comes the confusion for the compiler:
This definitely doesn work:

int a = 10
int **b = &(&a);
....
compiler says correctly: *& requires l-value*

So if cout <<&a[0]; goes thru that means a[0] is l-value;
Then the following has to work as well:
int a[2][2] = {...}
int b[2] = {...}
a[0] = b;

MS VC compiler reports error: '=' : left operand must be l-value

so a[0] *is not* a l-value;

Can you see the inconsistency here.

I believe you will get different results with different compiler.

HTH
B\rgds
100

Nov 15 '05 #3
100
Hi Wajih-ur-Rehman,

"> The question is actually about 2D arrays. As far as I know, when a 2D
array
is declared in C++, in the memory it is actually stored as a 1D array. and
the name of the array is pointing to the first element ie. a[0][0] i.e

int a[2][2] = { {1,2},{3,4} };

Now the question is that why cant i do this
int *p = a; //Compiler gives error. Whats wrong with this????? a is not an
int ** right?
No it is not.
Actually multidimensional arrays are treated differently when they has been
defined statically. They are array of arrays of arrays......
But it is not int** because no pointers are saved so it is not int **.

So when you do a[0] it returns one-dimensional array for the first row,
which can be cast to int *
However following are correct
int *p = &a[0]; This will fail (check it). a[0] is onedimensional array and it is already
pointer to the first element.
So what a[0] returns is address (number, constant) you cannot get the
address of a number. You can get address only of *l-values* (varaibles)
the correct is:
int *p = a[0];

Unlike a[0], which is not a *l-value* a[0][0] is a normal *l-value* and you
can get its address. So the following succeed. int *p = &a[0][0]; This is ok. a[0][0] returns a *l-value* of the first element and &a[0][0]
returns its address

Why doesnt int* p = a; work? (if you cout<<a<<&a[0]<<&a[0][0]; it gives the same address)


It shouldn't work. you cannot get &a[0]. However VC++ compiles it and just
change the type of the returning address. now it is of type int (*)[2] which
means pointer that points to array of 2 int values.
I don't know if it is by C/C++ spec and frankly I don't believe it.
Sometimes designers of the compilers simplify the grammer in order to make
it simple and the compiler faster. This simplifications lead to some really
rear erronous constructs to go thru and compiler may not report error at all
or may report not correct error.

So I believe:
int **b = &a[0] ;
should be reported as the error "'&' requires l-value";

Indexing operator [] works differently for multidimensional arrays when they
are declared statically and dynamically:
int a[2][2]
or
int **a;

And here comes the confusion for the compiler:
This definitely doesn work:

int a = 10
int **b = &(&a);
....
compiler says correctly: *& requires l-value*

So if cout <<&a[0]; goes thru that means a[0] is l-value;
Then the following has to work as well:
int a[2][2] = {...}
int b[2] = {...}
a[0] = b;

MS VC compiler reports error: '=' : left operand must be l-value

so a[0] *is not* a l-value;

Can you see the inconsistency here.

I believe you will get different results with different compiler.

HTH
B\rgds
100

Nov 15 '05 #4
100
Hi Wajih-ur-Rehman,

"> The question is actually about 2D arrays. As far as I know, when a 2D
array
is declared in C++, in the memory it is actually stored as a 1D array. and
the name of the array is pointing to the first element ie. a[0][0] i.e

int a[2][2] = { {1,2},{3,4} };

Now the question is that why cant i do this
int *p = a; //Compiler gives error. Whats wrong with this????? a is not an
int ** right?
No it is not.
Actually multidimensional arrays are treated differently when they has been
defined statically. They are array of arrays of arrays......
But it is not int** because no pointers are saved so it is not int **.

So when you do a[0] it returns one-dimensional array for the first row,
which can be cast to int *
However following are correct
int *p = &a[0]; This will fail (check it). a[0] is onedimensional array and it is already
pointer to the first element.
So what a[0] returns is address (number, constant) you cannot get the
address of a number. You can get address only of *l-values* (varaibles)
the correct is:
int *p = a[0];

Unlike a[0], which is not a *l-value* a[0][0] is a normal *l-value* and you
can get its address. So the following succeed. int *p = &a[0][0]; This is ok. a[0][0] returns a *l-value* of the first element and &a[0][0]
returns its address

Why doesnt int* p = a; work? (if you cout<<a<<&a[0]<<&a[0][0]; it gives the same address)


It shouldn't work. you cannot get &a[0]. However VC++ compiles it and just
change the type of the returning address. now it is of type int (*)[2] which
means pointer that points to array of 2 int values.
I don't know if it is by C/C++ spec and frankly I don't believe it.
Sometimes designers of the compilers simplify the grammer in order to make
it simple and the compiler faster. This simplifications lead to some really
rear erronous constructs to go thru and compiler may not report error at all
or may report not correct error.

So I believe:
int **b = &a[0] ;
should be reported as the error "'&' requires l-value";

Indexing operator [] works differently for multidimensional arrays when they
are declared statically and dynamically:
int a[2][2]
or
int **a;

And here comes the confusion for the compiler:
This definitely doesn work:

int a = 10
int **b = &(&a);
....
compiler says correctly: *& requires l-value*

So if cout <<&a[0]; goes thru that means a[0] is l-value;
Then the following has to work as well:
int a[2][2] = {...}
int b[2] = {...}
a[0] = b;

MS VC compiler reports error: '=' : left operand must be l-value

so a[0] *is not* a l-value;

Can you see the inconsistency here.

I believe you will get different results with different compiler.

HTH
B\rgds
100
Nov 15 '05 #5
100
Hi Wajih-ur-Rehman,

"> The question is actually about 2D arrays. As far as I know, when a 2D
array
is declared in C++, in the memory it is actually stored as a 1D array. and
the name of the array is pointing to the first element ie. a[0][0] i.e

int a[2][2] = { {1,2},{3,4} };

Now the question is that why cant i do this
int *p = a; //Compiler gives error. Whats wrong with this????? a is not an
int ** right?
No it is not.
Actually multidimensional arrays are treated differently when they has been
defined statically. They are array of arrays of arrays......
But it is not int** because no pointers are saved so it is not int **.

So when you do a[0] it returns one-dimensional array for the first row,
which can be cast to int *
However following are correct
int *p = &a[0]; This will fail (check it). a[0] is onedimensional array and it is already
pointer to the first element.
So what a[0] returns is address (number, constant) you cannot get the
address of a number. You can get address only of *l-values* (varaibles)
the correct is:
int *p = a[0];

Unlike a[0], which is not a *l-value* a[0][0] is a normal *l-value* and you
can get its address. So the following succeed. int *p = &a[0][0]; This is ok. a[0][0] returns a *l-value* of the first element and &a[0][0]
returns its address

Why doesnt int* p = a; work? (if you cout<<a<<&a[0]<<&a[0][0]; it gives the same address)


It shouldn't work. you cannot get &a[0]. However VC++ compiles it and just
change the type of the returning address. now it is of type int (*)[2] which
means pointer that points to array of 2 int values.
I don't know if it is by C/C++ spec and frankly I don't believe it.
Sometimes designers of the compilers simplify the grammer in order to make
it simple and the compiler faster. This simplifications lead to some really
rear erronous constructs to go thru and compiler may not report error at all
or may report not correct error.

So I believe:
int **b = &a[0] ;
should be reported as the error "'&' requires l-value";

Indexing operator [] works differently for multidimensional arrays when they
are declared statically and dynamically:
int a[2][2]
or
int **a;

And here comes the confusion for the compiler:
This definitely doesn work:

int a = 10
int **b = &(&a);
....
compiler says correctly: *& requires l-value*

So if cout <<&a[0]; goes thru that means a[0] is l-value;
Then the following has to work as well:
int a[2][2] = {...}
int b[2] = {...}
a[0] = b;

MS VC compiler reports error: '=' : left operand must be l-value

so a[0] *is not* a l-value;

Can you see the inconsistency here.

I believe you will get different results with different compiler.

HTH
B\rgds
100
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: regisser | last post by:
I have a quastion for the C++ professionals and members of the C++ standartization commetee. As i know C++ standart requires an allocator to be a templated parameter in every STL container....
7
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
7
by: rs | last post by:
Just out of idle curiosity: 1)Regarding pointers; I don't have the standard/grammar with me, but T* for type T mean pointer-to-T. Does that mean that with a declaration of type T** is actually...
9
by: Alfonso Morra | last post by:
Hi, I am having some probs with copying memory blocks around (part of a messaging library) and I would like some confirmation to make sure that I'm going about things the right way. I have...
2
by: akshayrao | last post by:
could someone point me in the right direction regarding this question?? http://groups.google.com/group/programpara/browse_thread/thread/75b58c897f890930 thanks.
3
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual...
21
by: Bo Yang | last post by:
As long as I write c++ code, I am worry about the pointer. The more the system is, the dangerous the pointer is I think. I must pass pointers erverywhere, and is there a way to ensure that every...
2
by: Renji Panicker | last post by:
I have a question regarding references. Consider the following code: <code> #include <iostream> class A { int _a1; int _a2; int _a3; };
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.