Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
but when i access the attributes of Structure i get the exception, the code i used is below

What I have tried:

i have structure name "Guitar", code that is used to create 2 D array of Guitar Objects
C++
Guitar ** guitarlists = (Guitar **)malloc(sizeof(Guitar *));
 for (int i = 0; i < row; i++)
 {
     guitarlists[i] = (Guitar *)malloc(sizeof(Guitar));
 }
Posted
Updated 17-May-18 3:25am
v2

Quote:
Guitar ** guitarlists = (Guitar **)malloc(sizeof(Guitar *));

That should be
C
Guitar ** guitarlists = (Guitar **)malloc(row * sizeof(Guitar *));


[update]
As Thaddeus Jones correctly pointed out, if you need a bidimensional array of Guitar then you have to modify
Quote:
guitarlists[i] = (Guitar *)malloc(sizeof(Guitar));
as well.
[/update]


Please note: since you apparently are using C++, why don't you use C++ programming language and standard library features (e.g. containers)?
 
Share this answer
 
v2
If you want a 2D array of r rows and c columns, you can do this:

Guitar ** guitarlists = (Guitar **)malloc(r* sizeof(Guitar *));
for (int i = 0; i < r; i++)
{
	guitarlists[i] = (Guitar *)malloc(c * sizeof(Guitar));
}
 
Share this answer
 
Comments
CPallini 17-May-18 7:16am    
Damn, I have to update my solution. :-)
5.
In C++ it is standard to use the "new" new and delete operators like that:
C++
Guitar *guitarlists = new Guitar[row];//normal array
delete guitarlists; 
Or am I missing some bits?
 
Share this answer
 

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