I have a confused, see the following code:
#include
using namespace std;
int main(void){
int a[4]={1,2,3,4}; // 1D int array.
int a2[3][4]={{1,2,3,4},{5,7,8,9},{10,11,12}}; // 2D int array.
int (*p)[4]=NULL;
// Their address is:
cout<<"a="< cout<<"&a="<<&a<
{
cout<<"&a["< }
cout<<"\n\n";
cout<<"a2="<
for (int i=0;i<3;i++)
{
for (int j=0;j<4;j++)
{
cout<<"&a2["< }
cout<
cout<<"\n\n";
/*
The output is:
a=002EFDAC
&a=002EFDAC
&a[0]=002EFDAC &a[1]=002EFDB0 &a[2]=002EFDB4 &a[3]=002EFDB8
a2=002EFD74
&a2=002EFD74
&a2[0][0]=002EFD74 &a2[0][1]=002EFD78 &a2[0][2]=002EFD7C &a2[0][3]=002EFD80
&a2[1][0]=002EFD84 &a2[1][1]=002EFD88 &a2[1][2]=002EFD8C &a2[1][3]=002EFD90
&a2[2][0]=002EFD94 &a2[2][1]=002EFD98 &a2[2][2]=002EFD9C &a2[2][3]=002EFDA0
We found the following equivalence relation(memory address):
1)a=&a=&a[0]=002EFDAC
2)a2=&a2=&a2[0][0]=002EFD74
*/
// Okay, time for a quiz:
p=a; // Error C2440: "=": not available from "int [4]" convert "int (*) [4]".
p=&a; // This line is correct.we know that a and &a are equal(memory address),why p=a is error?
p=a2; // This line is correct.
system("pause");
return 1;
}
Thank.
Regards
Ken.

Pankaj Kumar ChoudharyPosted Mar 22, 2015, 3:56 AM
p=a; // it produce an error
p=&a; // does not produce an error
p=a2; // not produce an error.
Now am trying to explain all above.
p=a; and p=&a here a and &a both have same address. but when compiler will compile this code
p=a;
here "a" represent the address of first element of array. so when compiler will compile this code then compiler will assume that we are providing a address of single element to " pointer to array" because "a" represent address of a[0] element so
compiler will throw an exception.
when compiler execute the
p=&a;
then compiler will treat as that we are providing an starting address of an array which length
is equal to 4.
So compiler will not produce any error.
In case of
p=a2;
as i explain above compiler will treat as that we are providing the address of starting element of array a2.
"here starting element of array a2 a2[0] is 1D array which length is equal to 4 so compiler
will treat as that we are providing the starting address of array a2[0] which length is 4.
So compiler will not produce any array.
If we change the array a2 as following
int a2[3][5]={{1,2,3,4,65},{5,7,8,9,98},{10,11,12,12,65}}; but now
p=a2
will throw an error that
"cannot convert int[5] * to int[4] * "
because
when we use p=a2; compiler will treat this statement as that we are providing the address of first element of a2 .here first element of a2 is an array which length is "5" so it will throw an error because p is pointer to an array which length is 4.
if any doubt related to it then you can post....
Gowtham RajamanickamPosted Apr 5, 2015, 11:25 PM
Pankaj Kumar ChoudharyPosted Mar 22, 2015, 12:47 PM
cout<<"a sizeof="<
the output is a compiler dependent .it depend upon that how a compiler treat a code.
in c compiler take the whole sizeof an static array .
static array mean an array whose size is known at compile time in our code a,a2 are static array.
if you generate an array dynamic
like as
int *m;
m=new int[4]; and set
m[0]=10;
m[1]=11;
m[2]=12;
m[3]=42;
if you take sizeof an dynamic create array
cout<
cout<
4
cout<
4
so it depent upon Behavioure of a compiler some time we don't get o/p as we expected.
Pankaj Kumar ChoudharyPosted Mar 22, 2015, 12:20 PM
void call(int *p)
{
for(int i=0;i<4;i++)
cout<<*(p+i)<<" ";
}
void call2(int (*p)[4))
{
for(int i=0;i<4;i++)
cout<<*(*(p+0)+i)<<" ";
}
now in main method .
we we use
call(a)
then o/p will 1 2 3 4
because call method pass a address of starting element(single element) and pointer p accept this address
but if we use
call(&a);
then compiler will throw an exception
"cannot convert int[4] * to int *"
because now method call pass a staring address of array of size[4] which can not be accepted.
If you want to pass "&a" then you should use method call2 because it accept of staring address of array of size 4
in case of
call(a2)
give correct o/p
but we can not use
call2(&a2)
because it will pass the staring address of a 2d array of size [4][3]
so a,&a,a2,&a2 behave differently it depend upon us how we want to use them.
VulpesPosted Mar 22, 2015, 12:19 PM
It returns the storage in bytes required by its operand which can be a type, a variable name or a more complex expression.
So if you pass it the name of a static array i.e. an array whose size is known at compile time such as 'a' then it will return the size of the whole array. This is an exception to the normal rule that the name of an array generates a pointer to the first element of the array.
However, if you pass it an expression such as '&a' then it will return the size of that expression, namely the size of a memory address which is, of course, 4 bytes on a 32 bit system.
C-style arrays are always laid out contiguously in memory though, if it's an array of pointers, those pointers could of course point to arbitrary addresses where the individual values are stored.
Ken HPosted Mar 22, 2015, 11:39 AM
int *p3=&a[0]; // this is correct.
for (int i=0;i<4;i++)
{
cout<<"*(p2+"< }
cout<<"\n\n";
for (int i=0;i<4;i++)
{
cout<<"p3["< }
cout<<"\n\n";
As expected, the output is:
*(p2+0)=1 *(p2+1)=2 *(p2+2)=3 *(p2+3)=4
p3[0]=1 p3[1]=2 p3[2]=3 p3[3]=4
Can you explain 'array name', '&array name' respectively, in the 1D and 2D+ array of difference and usage of it?
such as: a,&a,a2,&a2
Because of their addresses are the same. I need to know them in memory layout and use of the occasion.
Ken HPosted Mar 22, 2015, 10:51 AM
I can understand is:
1)&a represent as starting address of an array in 1D array?
2)a2 represent as starting address of an array in 2D array?
Below failed to achieve the desired results:
cout<<"a sizeof="<
cout<<"a2 sizeof="<
Thank.