Click here to Skip to main content
15,922,574 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1.Is this valid;

int * p = new int [4][5];

if not why;
(i.e. is dynamically allocating multidimensional array allowed?)

2. How does one return the address of a variable in dynamically allocated array;
Is the following valid?

class MyClass
{
int *p;
MyClass();
int Func();
};

MyClass::MyClass()
{
p = new int [3][4];
}



int *MyClass::Func()
{


return &p[3];//This is the line in doubt
}
Posted
Updated 3-May-14 2:30am
v4

1 solution

This looks like your homework to me, but...
1) Try it! Add that line to your favorite C / C++ compiler, and build it. Does the compiler complain?

2) Well, the compiler won't complain...but that doesn't mean it's a good idea. See here: http://en.wikipedia.org/wiki/Dangling_pointer[^]
 
Share this answer
 
Comments
Gbenbam 3-May-14 8:06am    
I'm sorry, I dont understand your solution could you kindly be more explicit?
OriginalGriff 3-May-14 8:28am    
What part is confusing you?
Gbenbam 3-May-14 9:27am    
1.I do not have access to a pc at d moment.Pls is the answer yes or no.

2.If my goal is to return the address of a variable in an. array of a. dunamically allocated variable, if the above approach correct, given that Func is a member of MyClass and the object on which func is called exist throught the time the app remains runng.(See my midification.
OriginalGriff 3-May-14 9:40am    
You are trying to code without access to a compiler?
Don't - particularly when you are just starting.
Wait until you are back at your PC before trying to do your homework.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900