dynamic allocation
int(*p)[5];
what this statement does..?????
does it allocate some memory or does it just declare p......what it actually does ..???i am new to c++ please help
>
int(*p)[5];
p
is an (uninitialized) pointer to array of 5
int
.
You could now do:
1 2
|
int array[5] = {0} ;
p = &array ;
|
yes it allocate some memory. try this
int (*p)[5];
cout<<"Address "<<&(*p)[0]<<endl;
cout<<"Address "<<&(*p)[1]<<endl;
cout<<"Address "<<&(*p)[2]<<endl;
cout<<"Address "<<&(*p)[3]<<endl;
cout<<"Address "<<&(*p)[4]<<endl;
int (*p)[5];
does not allocate memory; it is, as JLBorge said, a pointer (to an array of int of size 5.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <iostream>
#include <typeinfo> // for 'typeid' to work
using namespace std;
int main()
{
int (*p)[5];
int* q[5];
int r[5];
cout << "type of p = " << typeid(p).name() << endl;
cout << "type of q = " << typeid(q).name() << endl;
cout << "type of r = " << typeid(r).name() << endl;
cout << endl;
cout << "sizeof p = " << sizeof(p) << endl;
cout << "sizeof q = " << sizeof(q) << endl;
cout << "sizeof r = " << sizeof(r) << endl;
cout << endl;
return 0;
}
|
Output:
type of p = int (*)[5]
type of q = int * [5]
type of r = int [5]
sizeof p = 4
sizeof q = 20
sizeof r = 20 |
So you can see that p has a size of
just 4 bytes (the size of a pointer when compiling 32-bit, as I did.)
Andy
PS @jain amit 14
Please see:
How to use code tags
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Topic archived. No new replies allowed.